Operations on Numbers

The following operations can be applied to all numeric types:

Operation Description
x + y Addition
x - y Subtraction
x * y Multiplication
x / y Division
x ** y Power (x y )
x % y Modulo (x mod y )
-x Unary minus
+x Unary plus

For integers, division truncates the result to an integer. Thus, 7/4 is 1, not 1.75. 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 - int(x / y ) * y . For complex numbers, the modulo operator returns x - int((x / y ).real ) * y .

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

Get Python Essential Reference, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.