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 | Truncating division |
x ** y | Power (xy) |
x % y | Modulo (x mod y) |
–x | Unary minus |
+x | 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. As of this writing, the true division operator (/) also truncates the result to an integer if the operands are integers. Therefore, 7/4 is 1, not 1.75. However, this behavior is scheduled to change in a future version of Python, so you will need to be careful. The modulo operator returns the remainder of the division x // y. For example, 7 % ...
Get Python: Essential Reference, Third 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.