Loading lessons...
Arithmetic Operators
Arithmetic Operators
Arithmetic operators do math with numbers.
| Operator | Name | Example |
|---|---|---|
+ | Addition | x + y |
- | Subtraction | x - y |
* | Multiplication | x * y |
/ | Division | x / y |
% | Modulus (remainder) | x % y |
** | Exponentiation | x ** y |
// | Floor division | x // y |
The tricky ones
/ always returns a float:
print(10 / 3) # 3.3333333333333335
print(8 / 2) # 4.0
// divides and rounds down to a whole number:
print(10 // 3) # 3
print(-10 // 3) # -4 (rounds down, not toward zero)
% gives the remainder:
print(10 % 3) # 1
print(7 % 2) # 1 (odd)
print(8 % 2) # 0 (even)
** raises to a power:
print(2 ** 3) # 8
Division by zero
Dividing by zero raises ZeroDivisionError:
print(1 / 0) # ZeroDivisionError
TL;DR
/is float division,//is floor division.%is the remainder,**is power.- Modulus is great for even/odd checks.
- Dividing by zero errors out.