An operator is a symbol that does something to values. You already used + and =. This lesson covers the rest of the everyday ones.
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | add | 7 + 2 | 9 |
- | subtract | 7 - 2 | 5 |
* | multiply | 7 * 2 | 14 |
/ | divide (always a float) | 7 / 2 | 3.5 |
// | floor divide (drops the remainder) | 7 // 2 | 3 |
% | modulo (the remainder) | 7 % 2 | 1 |
** | power | 2 ** 10 | 1024 |
// and % work as a pair. If you divide 17 cookies among 5 people, 17 // 5 is how many each person gets (3) and 17 % 5 is how many are left over (2).
total_seconds = 125
minutes = total_seconds // 60 # 2
seconds = total_seconds % 60 # 5A very common trick: n % 2 is 0 for even numbers and 1 for odd ones.
Warning
Floats can be slightly off because computers store them in binary. 0.1 + 0.2 gives 0.30000000000000004. Use round() when you print decimals.
What does floor division do with negative numbers?
// always rounds down toward negative infinity, so -7 // 2 is -4, not -3. This surprises people at first, but it keeps // and % consistent with each other.
Comparison operators
Comparisons produce a bool: True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | equal to | 5 == 5 | True |
!= | not equal to | 5 != 5 | False |
< | less than | 3 < 5 | True |
> | greater than | 3 > 5 | False |
<= | less than or equal | 5 <= 5 | True |
>= | greater than or equal | 4 >= 5 | False |
Warning
One equals sign (=) assigns a value. Two (==) compares. Mixing them up is the most common beginner mistake.
You can chain comparisons like in math:
age = 25
print(18 <= age < 65) # TrueAssignment operators
Updating a variable based on its own value is so common that Python has shortcuts:
| Shortcut | Same as |
|---|---|
x += 3 | x = x + 3 |
x -= 3 | x = x - 3 |
x *= 3 | x = x * 3 |
x /= 3 | x = x / 3 |
x //= 3 | x = x // 3 |
x %= 3 | x = x % 3 |
x **= 3 | x = x ** 3 |
lives = 3
lives -= 1
print(lives) # 2Logical operators
Logical operators combine bool values.
| Operator | Meaning | Example | Result |
|---|---|---|---|
and | both must be true | True and False | False |
or | at least one must be true | True or False | True |
not | flips the value | not True | False |
age = 20
has_ticket = True
print(age >= 18 and has_ticket) # True
print(age < 13 or age > 65) # False
print(not has_ticket) # FalseOperator precedence
When an expression has several operators, Python follows an order, just like in math class. From highest to lowest:
| Order | Operators |
|---|---|
| 1 | ( ) parentheses |
| 2 | ** |
| 3 | *, /, //, % |
| 4 | +, - |
| 5 | ==, !=, <, >, <=, >= |
| 6 | not |
| 7 | and |
| 8 | or |
print(2 + 3 * 4) # 14, multiplication goes first
print((2 + 3) * 4) # 20, parentheses win
print(2 ** 3 ** 2) # 512, ** is evaluated right to left
print(-2 ** 2) # -4, the power happens before the minusTip
If you're not sure about the order, add parentheses. They never hurt and make your code easier to read.
Now try it yourself. The next step is two puzzles that use %, //, and comparisons.