2.1.3.6 Operators - data manipulation tools

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.

The subtraction operator, unary and binary operators

The subtraction operator is obviously the - (minus) sign, although you should note that this operator also 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 the 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 above - can you guess its output?


  • Console 

Comments

Popular Posts