"Python operators" is one of the most searched programming phrases by school and first-year students, usually when a homework answer has come out wrong and the reason is a / that should have been a //, an "or" that does not mean what English means by or, or a minus sign that the power took first. This page is the lesson: every operator a school or introductory course uses, with the exact result of each, the truth tables and short-circuit rule for and, or and not, the precedence table, the five mistakes that produce most wrong answers, and twelve practice questions with answers. The printable is a one-page cheat sheet with the practice on the back.
Download the Python operators cheat sheet (PDF) (every operator with an example and its result, the truth tables, the precedence table, the five mistakes, and twelve practice questions with answers; two pages, free).
1. Arithmetic operators
| Operator | Name | Example | Result | Note |
|---|---|---|---|---|
| + | Addition | 7 + 2 | 9 | Also joins strings and lists: "ab" + "cd" is "abcd" |
| − | Subtraction | 7 − 2 | 5 | |
| * | Multiplication | 7 * 2 | 14 | Also repeats: "ab" * 3 is "ababab" |
| / | True division | 7 / 2 | 3.5 | Always a float, even 6 / 3, which is 2.0 |
| // | Floor division | 7 // 2 | 3 | Rounds down: −7 // 2 is −4, not −3 |
| % | Modulo (remainder) | 7 % 2 | 1 | n % 2 == 0 tests for even; 17 % 5 is 2 |
| ** | Power | 2 ** 10 | 1024 | 9 ** 0.5 is 3.0 (a square root) |
Two consequences students meet in the first week. Division always makes a float, so a program that counts things with / ends up with 4.0 items; use // when the answer must be a whole number. And % with // together split any division into quotient and remainder (17 // 5 is 3, 17 % 5 is 2, and 3 × 5 + 2 is 17), which is the basis of every "last digit", "is it divisible", "convert to binary" and "wrap around a clock" problem.
2. Comparison operators
| Operator | Means | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5.0 | True (the values are equal) |
| != | Not equal to | 5 != 6 | True |
| < | Less than | 3 < 5 | True |
| > | Greater than | 3 > 5 | False |
| <= | Less than or equal | 5 <= 5 | True |
| >= | Greater than or equal | 4 >= 5 | False |
Every comparison gives a Boolean, True or False, and comparisons can be chained: 1 < x < 10 means 1 < x and x < 10, which most languages do not allow and which reads like mathematics. Strings compare alphabetically by character code ("apple" < "banana" is True, and "Zebra" < "apple" is also True, because capitals come first).
3. Logical operators: and, or, not
| a | b | a and b | a or b | not a |
|---|---|---|---|---|
| True | True | True | True | False |
| True | False | False | True | False |
| False | True | False | True | True |
| False | False | False | False | True |
and is True only when both sides are True. or is True when at least one side is True (this is the inclusive or: both True also gives True, unlike the everyday English "tea or coffee"). not flips.
Short-circuiting. Python evaluates left to right and stops as soon as the result is known. In x != 0 and total / x > 2, when x is 0 the left side is False, the whole and must be False, and the division on the right is never attempted, so there is no error. In name == "" or name[0] == "A", an empty name makes the left side True and the right side, which would crash on an empty string, is skipped. Put the cheap or protective test first.
What they return. and and or return the value that decided the result, not necessarily True or False: 0 or 5 is 5, "" or "default" is "default", 3 and 7 is 7. Python treats 0, 0.0, "", [], None and False as false and everything else as true. This is why x = user_input or "guest" is a common idiom, and why beginners are surprised when printing an and expression shows a number.
4. Assignment operators
| Operator | Example | Same as |
|---|---|---|
| = | x = 5 | Puts 5 in x (not a comparison) |
| += | x += 3 | x = x + 3 |
| −= | x −= 3 | x = x − 3 |
| *= | x *= 2 | x = x * 2 |
| /= | x /= 2 | x = x / 2 (makes a float) |
| //= | x //= 2 | x = x // 2 |
| %= | x %= 2 | x = x % 2 |
| **= | x **= 2 | x = x ** 2 |
Python also allows multiple assignment (a, b = 1, 2) and swapping without a temporary variable (a, b = b, a), which every other introductory language needs three lines for.
5. Membership and identity
| Operator | Means | Example | Result |
|---|---|---|---|
| in | Is it one of the items? | 3 in [1, 2, 3] | True |
| not in | Is it absent? | "x" not in "python" | True |
| is | Is it the same object? | a is b | True only if a and b are the very same object in memory |
| is not | Not the same object | a is not None | The standard way to test for None |
in works on lists, strings (substrings: "th" in "python" is True), tuples, sets and dictionary keys. is compares identity, not value: two separate lists with the same contents are == but not is. Use == for values and is only for None, True and False.
6. The order of precedence
From highest (done first) to lowest, when there are no brackets:
| Level | Operators | Example |
|---|---|---|
| 1 | ** | 2 ** 3 ** 2 is 2 ** 9 = 512 (powers group from the right) |
| 2 | unary −, + | −2 ** 2 is −(2 ** 2) = −4 |
| 3 | * / // % | 7 + 6 // 2 is 7 + 3 = 10 |
| 4 | + − | |
| 5 | comparisons: == != < > <= >= in, not in, is, is not | 1 + 1 == 2 is True; they chain |
| 6 | not | not 1 == 2 is not (1 == 2) = True |
| 7 | and | |
| 8 | or | True or False and False is True or (False and False) = True |
The practical rule: brackets whenever a reader might pause. (a and b) or c costs two characters and prevents the commonest logic bug in beginner code.
The five mistakes beginners make
- = for ==.
if x = 5:is a syntax error;x == 5on its own line does nothing. Read = as "becomes" and == as "equals". - "x == 1 or 2". English says "if x is 1 or 2"; Python reads
(x == 1) or 2, and since 2 is true, the condition is always True. Writex == 1 or x == 2, orx in (1, 2). - is for ==.
if name is "Alice"may work by accident and fail later. Values compare with ==;isis for None. - / when // was meant.
middle = length / 2gives 2.5 for a list of five and crashes when used as an index. Use //. - Trusting precedence with not and and.
not a and bis(not a) and b, notnot (a and b). Bracket it.
Practice: twelve questions
Write the result of each.
- 17 // 5 and 17 % 5
- 2 ** 3 ** 2
- −3 ** 2
- 7 / 2 == 3.5
- 10 % 3 == 1 and 10 // 3 == 3
- not True or True
- 5 > 3 > 1
- "a" in "banana" and "x" not in "banana"
- 0 or "empty"
- x = 4; x += 3; x *= 2; x
- (3 > 2) + (5 == 5)
- True or 1 / 0
Answers: 1) 3 and 2. 2) 512. 3) −9. 4) True. 5) True. 6) True (not binds first: (not True) or True). 7) True (chained). 8) True. 9) "empty". 10) 14. 11) 2 (True is 1 in arithmetic). 12) True, with no error: or short-circuits before the division.
When a child should start Python rather than Scratch, and how the course runs, is in Scratch or Python: which first.
Download the Python operators cheat sheet (PDF)
Where to get help
A 1-on-1 online Python class with a tutor who holds a master's in computer science: the child codes in a shared editor while the tutor watches every line, each operator met inside a small program (a clock, a divisibility checker, a guessing game) rather than a list, and the school's own assignments (GCSE Computer Science, AP Computer Science Principles, CBSE Class 11 and 12 Informatics Practices, IB) worked together. The recording after every class, the same tutor all year, US$10 to $13 an hour. The free 30-minute first class is a real lesson: the child writes a working program before it ends.
Questions parents ask
1What are the operators in Python?
2What is the difference between / and // in Python?
3How do and, or and not work in Python?
4What is the difference between == and = in Python?
5What is operator precedence in Python?
See how we teach this, 1-on-1 online →
Shobha
Founder and lead tutor, Science with Shobha
Teaching maths, science and English to children online since 2018. 500+ students, 20+ tutors, families in nine countries.
