Operations on Numbers

The following operations can be applied to all numeric types:

OperationDescription
x + yAddition
x - ySubtraction
x * yMultiplication
x / yDivision
x // yTruncating division
x ** yPower (xy)
x % yModulo (x mod y)
–xUnary minus
+xUnary 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.