Operators & expressions
Operators combine values into new ones. Most behave like school maths, but a few (division, equality versus identity, and how and/or work) trip people up in exams and in real code.
Arithmetic
7 + 2 # 9
7 - 2 # 5
7 * 2 # 14
7 / 2 # 3.5 (/ always gives a float)
7 // 2 # 3 (floor division: drop the remainder)
7 % 2 # 1 (modulo: just the remainder)
2 ** 3 # 8 (power)
% is more useful than it looks: n % 2 == 0 tests for even numbers, and i % 10 == 0 does something every tenth time in a loop. + also joins strings and lists, and "ab" * 3 repeats a string ('ababab').
Order of operations: ** first, then * / // %, then + -, left to right. 2 + 3 * 4 is 14; use brackets when in doubt: (2 + 3) * 4 is 20. Shortcuts like count += 1 mean count = count + 1.
Comparisons
== != < > <= >= compare values and give True or False. They can be chained: 1 < x < 10. The in operator tests membership in strings, lists, sets and dict keys: "prod" in env_name, port in [80, 443].
== versus is
== asks "do these have the same value?"; is asks "are these the very same object in memory?".
a = [1, 2, 3]
b = [1, 2, 3]
a == b # True: same contents
a is b # False: two separate lists
Use == for comparing values. The one standard use of is is checking for None: if result is None:.
Boolean logic and truthiness
and is true only if both sides are, or if either is, not flips a value. They short-circuit: Python stops as soon as the answer is known, so in user and user.is_admin the second part never runs if user is None.
Every value also counts as true or false in an if. "Falsy" values are False, None, 0, empty strings, and empty lists/dicts/sets; everything else is "truthy". That's why if errors: works as "if the list has anything in it".
Indexing and slicing
Strings and lists are numbered from 0. Negative numbers count from the end: items[-1] is the last one. A slice items[start:stop] includes start but stops before stop, so items[1:3] gives positions 1 and 2. Leave a side empty to go to the edge: items[:3] is the first three, items[::-1] is reversed.
s[1:4] is three items.Operators at work
Here's a small, realistic calculation: how full is a disk, as a percentage with one decimal?
used_gb = 412
total_gb = 500
percent = used_gb / total_gb * 100 # 82.39999999999999
print(f"Disk is {round(percent, 1)}% full") # Disk is 82.4% full
print("warning" if percent > 80 else "ok") # warning: a one-line if/else
A few built-in functions do a lot of everyday maths for you: round(x, 1), min(a, b), max(list), sum(list) and abs(-5).
Common mistakes
=versus==: one equals sign stores a value, two compare.if status = "ok":is aSyntaxError; you meant==.- Decimals aren't exact:
0.1 + 0.2gives0.30000000000000004, because computers store decimals in binary. Round before you display, and don't test floats with==; useabs(a - b) < 0.0001. - Text that looks like a number:
"10" == 10isFalse, and"10" < 5is aTypeError. Values from files,input()and command arguments are text until you convert them. - Floor division with negatives:
-7 // 2is-4, not-3. It rounds down, towards minus infinity.
Try it yourself
Write one line that is
Trueonly whenportis a valid port number (1 to 65535).Show an answer
1 <= port <= 65535Chained comparisons read just like maths.
filename = "backup-2026-09-30.tar.gz". Use slicing to get the date part2026-09-30.Show an answer
filename[7:17]Count from 0: the date starts at position 7 and the slice stops before 17.
Without running it, what do these give:
7 // 2,7 % 2,2 ** 10,bool("")?Show an answer
3,1,1024andFalse(an empty string is falsy).