Operators
print(7 + 2, 7 - 2, 7 * 2)9 5 14
+, - and * work just like on a calculator.
1Level 1 · BasicsBasics
Operators are the symbols that do work on values: adding, comparing, combining conditions. You already know most of them from school maths.
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | add | 7 + 2 | 9 |
- | subtract | 7 - 2 | 5 |
* | multiply | 7 * 2 | 14 |
/ | divide (always gives a float) | 7 / 2 | 3.5 |
// | floor division (whole part) | 7 // 2 | 3 |
% | remainder (modulo) | 7 % 2 | 1 |
** | power | 7 ** 2 | 49 |
// and % are a team. Think of sharing 7 sweets between 2 children: each gets 7 // 2 = 3, and 7 % 2 = 1 is left over.
Order of operations
Python follows the usual maths rules: ** first, then * / // %, then + -. So 2 + 3 * 4 is 14. Use brackets when in doubt: (2 + 3) * 4 is 20.
Comparison operators
These compare two values and give a bool (True or False): == equal, != not equal, <, >, <=, >=. Don't confuse = (store a value) with == (ask whether two values are equal).
Logical operators
and: True only if both sides are True.or: True if at least one side is True.not: flips True to False and back.
Python also lets you chain comparisons like in maths: 0 <= marks <= 100.
Key points:/gives a float,//the whole part,%the remainder; comparisons give True/False; combine them with and, or, not.
See it in actionSharing 17 sweets among 5 children
// gives each child's share and % gives what's left over.
Try it yourselfExample 1: All the arithmetic operators
Compare / with //, and look at what % gives.
Output appears hereSquaring a number
This should print the square of 9, which is 81, but it prints the wrong number. Fix it.
Example 2: Splitting minutes into hours
// and % together turn a total into "how many whole units" and "what's left over".
Output appears hereHours and minutes
135 minutes is 2 hours and 15 minutes. Fill the blanks with the right operators.
minutes = 135 print(minutes 60, "h", minutes 60, "min")
Example 3: Comparisons give True or False
Every comparison is a question with a yes/no answer.
Output appears hereCan they ride?
A ride needs height at least 120 cm and age at least 8. Fill the blanks so the program prints True.
height = 125 age = 9 print(height 120 age >= 8)
Even or odd test
Read a whole number and print True if it is even, otherwise False. Use the % operator.
Example input14Expected outputTrue
Example 4: and, or, not
Change age and has_ticket to see how each expression changes.
Output appears hereAverage of three
Three test scores are 78, 85 and 92. Print their average as a whole number using integer division: Average: 85.
2Level 2 · IntermediateChained comparisons and short-circuiting
Python's operators have a few habits that make code shorter and safer.
Chained comparisons
0 <= marks <= 100 means 0 <= marks and marks <= 100. It reads like maths.
Short-circuit evaluation
and stops at the first falsy value, or stops at the first truthy one. So count > 0 and total / count > 50 never divides by zero. or also gives defaults: name = typed or "Guest".
Membership
in and not in test whether something is inside a string: "a" in "cat" is True. Later they work on lists and other collections too.
See it in actionand stops early
With count = 0 the division on the right is never reached. Watch the arrow skip it.
Example 5: Range checks and safe division
The chained checks read like maths. With count = 0 the division never runs, so there is no error.
Output appears here3Level 3 · AdvancedBitwise operators, precedence and the walrus
Bitwise operators
They work on the binary digits of integers: & (and), | (or), ^ (xor), ~ (not), << and >> (shift). n & 1 is 1 for odd numbers; 1 << k is 2 to the power k. They are popular in contest problems and for storing on/off flags.
Precedence you must know
** binds tighter than a minus sign: -2 ** 2 is -4. not is weaker than comparisons, and and is stronger than or. When in doubt, add brackets.
Floor division with negatives
// rounds down, so -7 // 2 is -4 and -7 % 2 is 1. The rule a == (a // b) * b + a % b always holds.
The walrus operator :=
:= assigns and gives the value inside an expression: print((area := 4 * 5), area + 1) prints 20 21.
See it in actionShifting bits
Each << 1 doubles the number; watch the values grow.
Example 6: Bits at work
n & 1 is 1 for odd numbers and 0 for even ones. Each flag is one bit. | turns a bit on, & tests it.
Output appears hereExample 7: Precedence, negatives and :=
Work out each line on paper first, then run.
Output appears heren % 2 == 0 tests whether n is even, and n % 10 gives the last digit of a number. You'll use both constantly in practice problems.What does this print?
print(7 // 2 + 7 % 2)
50 graded problems are waiting in Topic Practice.