¡GoConqr estrena diseño! Cuéntanos qué te parece.

Módulo 18 de 24 · Texto

Operators: addition and subtraction

David Khieu

Operators: addition

The addition operator is the+ (plus)  sign, which is fully in line with mathematical standards. 

Again, take a look at the snippet of the program below: 

 

print(-4 + 4)

print(-4. + 8)

The result should be nothing surprising. Run the code to check it. 

0

4.0

The subtraction operator, unary and binary operators

The subtraction operator is obviously the - (minus) sign, although you should note that this operator has another meaning - it can change the sign of a number.

This is a great opportunity to present a very important distinction between unary and binary operators. 

In subtracting applications, the minus operator expects two arguments: the left (a minuend in arithmetical terms) and right (a subtrahend).

For this reason, the subtraction operator is considered to be one of the binary operators, just like the addition multiplication and division operators. 

But the minus operator may be used in a different (unary) way - take a look at the last line of snippet below: 

 

print(-4 - 4)
print(4. - 8)
print(-1.1)

By the way: there is also a unary + operator, You can use it like this: 

print(+2)

The operator preserves the sign of its only argument - the right one.

Although such a construction is syntactically correct, using it doesn't make much sense, and it would be hard to find a good rationale for doing so. 

Take a look at the snippet abor - can you guess its output? 

 

Instrucciones

Para avanzar en el curso, envía un intento de este test. Puedes enviarlo después de la última pregunta.

Ir al módulo elegido

Escríbenos

¿Sobre qué es tu mensaje?

¿Qué te parece el nuevo diseño?

¿Te gusta?