Arithmetic Operators in Python
Explore how to use Python's arithmetic operators for various mathematical operations. Learn the function of each operator, how in-place assignment operators work, and understand operator precedence and associativity to correctly evaluate expressions in Python.
We'll cover the following...
There are different arithmetic operators that can be applied for mathematical operations: +, -, *, /, %, // and **.
In-place assignment operators offer a good shortcut for arithmetic operations. They include +=, -=, *=, /=, %=, //= and **=.
Operation nuances
On performing floor division a // b, the result is the largest integer which is less than or equal to the quotient. The // operator is called a floor division operator.
In the code above:
- In
-10 // 3, the multiple of 3 that yields -10 is -3.333, whose floor value is-4. - In
10 // -3, the multiple of -3 that yields 10 is -3.333, whose floor value is-4. - In
-10 // -3, the multiple of -3 that yields -10 is 3.333, whose floor value is3.
Note: The
print()function is used for sending output to the screen.
Operation a % b is evaluated as ...