Operations on Numbers

The following operations can be applied to all numeric types:



x + y


x - y


x * y


x / y


x II y

Truncating division

x ** y

Power (xy)

x % y

Modulo (x mod y)


Unary minus


Unary plus

The truncating division operator (//, also known as floor division) truncates the result to an integer and works with both integers and floating-point numbers. In Python 2, the true division operator (/) also truncates the result to an integer if the operands are inte-gers.Therefore, 7/4 is 1, not 1.75. However, this behavior changes in Python 3, where division produces a floating-point result. The modulo operator returns the remainder of the division x // y. For example, 7 % 4 is 3. For floating-point numbers, the modulo operator returns the floating-point remainder of x // y, which is x - (x // y) * y. For complex numbers, the modulo (%) and truncating division operators (//) are invalid.

The following shifting and bitwise logical operators can be applied only to integers:

Operation x << y x >> y x & y x | y x * y

0 0

Post a comment