Numeric Type (Arithmetic) Operators

Python supports unary operators for no change and negation, + and -, respectively; and binary arithmetic operators +, -, *, /, %, and **, for addition, subtraction, multiplication, division, modulo, and exponentiation, respectively.
Rules and exceptions: Any zero right-hand argument for division and modulo will result in a ZeroDivisionError exception. Integer modulo is straightforward integer division remainder, while for float, take the difference of the dividend and the product of the divisor and the quotient of the quantity dividend divided by the divisor rounded down to the closest integer, i.e., x – (math.floor(x/y) * y.
For complex number modulo, take only the real component of the division result, i.e., x – (math.floor((x/y).real) * y).
The exponentiation operator has a peculiar precedence rule in its relationship with the unary operators: It binds more tightly than unary operators to its left, but less tightly than unary operators to its right. Due to this characteristic, you will find the ** operator twice in the numeric operator charts in this text. Here are some examples:

>>> 3 **2
>>> -3 ** 2
>>> (-3) ** 2
>>> 4.0 ** -1.0

In the second case, it performs 3 to the power of 2 (3-squared) before it applies the unary negation. We need to use the parentheses around the “-3” to prevent this from happening.


You may also like...