Arithmetic Operations
Python gives you the arithmetic you already know from math class, plus two operators that are new: floor division and modulus. This table is the whole set.
Arithmetic operations
Python gives you the arithmetic you already know from math class, plus two operators that are new: floor division and modulus. This table is the whole set.| Operator | Name | Description |
|---|---|---|
a + b | Addition | Sum of a and b |
a - b | Subtraction | Difference of a and b |
a * b | Multiplication | Product of a and b |
a / b | True division | Quotient of a and b |
a // b | Floor division | Quotient of a and b, removing fractional parts |
a % b | Modulus | Remainder after division of a by b |
a ** b | Exponentiation | a raised to the power of b |
-a | Negation | The negative of a |
+a | Unary plus | a unchanged (rarely used) |
This set covers addition through floor division. Modulus gets a set of its own next, and exponentiation and negation come after that.
Mixing types
An expression on two floats produces a float.When an expression's operands are an int and a float, Python automatically converts the int to a float.
True division vs floor division
The operator / is true division and the operator // returns floor division, which rounds down after the true divide. True divide / always gives the answer as a float.