Operators
Doing things to values
In this lesson
Operators are the symbols that combine and compare values. You’ve met several already; this lesson gathers them into one map and explains the order they run in.
Explain it like I’m 5
Operators are little verbs: add, compare, combine yes/no answers, check membership. Knowing the full set means you can express almost any simple idea in code.
The families of operators
letters = "aeiou"
print("e" in letters)
print("z" not in letters)
result = None
print(result is None)
True True True
Augmented assignment
count += 1 is shorthand for count = count + 1. The same pattern works for -=, *=, //=, and others. It’s less typing and makes ‘update this variable’ obvious.
total = 0
total += 5
total += 5
total *= 2
print(total)
20
Precedence: the order operators run
Like maths, Python has an order of operations: ** first, then * / // %, then + -, then comparisons, then not, then and, then or. So 2 + 3 * 4 is 14. When in doubt, add parentheses — they cost nothing and make intent obvious.
print(2 + 3 * 4)
print((2 + 3) * 4)
print(2 ** 3 * 2)
14 20 16
Predict the result, then run: combine a price calculation with a membership test. Add parentheses to make the maths clear.
price = 10
qty = 3
total = price * qty
print(total)
print(total > 25 and "discount" in "discount day")
and needs both sides True. Membership uses in.
price = 10
qty = 3
total = price * qty
print(total)
print((total > 25) and ("discount" in "discount day"))
30
True
The conditional expression (a one-line if/else)
Sometimes you want to choose a value based on a condition. Python has a compact operator for exactly that — the conditional expression (often called the ‘ternary’): value_if_true if condition else value_if_false.
It reads almost like English: status = "adult" if age >= 18 else "minor". Reach for it when the choice is short and clear; for anything with multiple steps, a full if statement is easier to read.
age = 16
status = "adult" if age >= 18 else "minor"
print(status)
for n in [4, 7]:
print(n, "is", "even" if n % 2 == 0 else "odd")
minor 4 is even 7 is odd
What does 2 + 3 * 4 evaluate to?
Multiplication runs before addition, so 3 * 4 = 12, then + 2 = 14.
What is the shorthand for x = x + 3?
+= is augmented assignment. =+ would just assign positive 3.
Mini exercise (medium)
Start with points = 0. Using augmented assignment, add 10, then add 5, then double it, and print the result.
Do it here. Finish the code below and press Run to try your answer right in the browser — no setup needed.
points = 0
# TODO: add 10, then add 5, then double it — using += and *=
print(points)
points += 10, points += 5, points *= 2.
points = 0
points += 10
points += 5
points *= 2
print(points)
30
__no_output_bypass__ = True
import ast as _ast
_augops = [type(n.op) for n in _ast.walk(_ast.parse(__user_code__)) if isinstance(n, _ast.AugAssign)]
assert _ast.Add in _augops, "use += to add to points"
assert _ast.Mult in _augops, "use *= to double points"
__no_output_bypass__ = False
assert points == 30, "expected 30"
print("✓ Correct!")