LearnPy Hub
BasicsBeginnerLesson 4 of 32

Operators

Watch the ideaOperators do the maths
print(7 + 2, 7 - 2, 7 * 2)
7 + 2 = 9
7 - 2 = 5
7 * 2 = 14
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

OperatorMeaningExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide (always gives a float)7 / 23.5
//floor division (whole part)7 // 23
%remainder (modulo)7 % 21
**power7 ** 249

// 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.

Step-by-step

Try it yourselfExample 1: All the arithmetic operators

Compare / with //, and look at what % gives.

example_1.py
Output appears here
Fix the bug

Squaring a number

This should print the square of 9, which is 81, but it prints the wrong number. Fix it.

buggy.py

Example 2: Splitting minutes into hours

// and % together turn a total into "how many whole units" and "what's left over".

example_2.py
Output appears here
Fill in the blanks

Hours and minutes

135 minutes is 2 hours and 15 minutes. Fill the blanks with the right operators.

blanks.py
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.

example_3.py
Output appears here
Fill in the blanks

Can they ride?

A ride needs height at least 120 cm and age at least 8. Fill the blanks so the program prints True.

blanks.py
height = 125
age = 9
print(height  120  age >= 8)
Your turn

Even or odd test

Read a whole number and print True if it is even, otherwise False. Use the % operator.

Example input14Expected outputTrue

my_code.py

Example 4: and, or, not

Change age and has_ticket to see how each expression changes.

example_4.py
Output appears here
Your turn

Average of three

Three test scores are 78, 85 and 92. Print their average as a whole number using integer division: Average: 85.

my_code.py

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.

Step-by-step

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.

example_5.py
Output appears here

3Level 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.

Step-by-step

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.

example_6.py
Output appears here

Example 7: Precedence, negatives and :=

Work out each line on paper first, then run.

example_7.py
Output appears here
Handy trickn % 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.
Exercise · predict the output

What does this print?

print(7 // 2 + 7 % 2)
Quick quiz · 5 questions
1. What is 10 / 2?
2. What is 2 + 3 * 4?
3. What does 23 % 5 give?
4. Which expression is True when x is 7?
5. What is the difference between = and ==?
Ready to practice operators?

50 graded problems are waiting in Topic Practice.

Practice this topic →
Python Operators · LearnPy Hub