Module 02 · Lesson 01
Operators & Expressions
Calculate with arithmetic operators, compare values, combine conditions with and, or and not, check membership with in, and know the order Python applies them in.
- About 8 minutes
- Beginner friendly
- 12-question quiz at the end
What you will learn
- Calculate and share
- Compare and combine
- Order of operations
- Watch the maths run
Python as a calculator
Operators are the symbols that do the work: they calculate, compare and combine values.
Calculate
+ - * /work like on a calculator.//keeps the whole part,%gives the leftover and**is a power.Compare
== != < > <= >=ask a question and answerTrueorFalse.Combine
and,orandnotjoin several True-or-False answers into one.Order
Brackets first, then
**, then* / // %, then+ -, then comparisons. Same idea as in maths.
Operators at work
A few symbols do a surprising amount of everyday work.
- Clocks
%wraps time around: 26 hours is 2 o'clock the next day, because26 % 24is 2. - Games
score >= 100decides when the next world unlocks. - Shops
price * quantityfills the basket total, and a comparison checks for free delivery. - Search boxes
"cat" in titlechecks whether a word appears in the text you are searching.
Which part goes first?
One line, three operators. Tap each part to see the order Python works in.
Part 1 of 4
show:Shows the final answer, after everything inside the brackets is worked out.
Prints True
Calculate, compare, combine
Pick an example, change it, then press Run. Say what you expect to see before you press.
Example 1 of 6Calculator
The four everyday operators. Notice / always gives a decimal.
Your turn: Work out how many seconds are in a day with one line of *.
print(7 + 2) print(7 - 2) print(7 * 2) print(7 / 2)
Press Run to see what it prints.Watch apples go into bags
Press Step to run one line at a time. Each bag holds 5 apples.
Ready. Press Step to run the first line.
- apples = 17
- bags = apples // 5
- left = apples % 5
- full = bags * 5 + left == apples
- print(bags, "bags,", left, "left")
- print(full)
Output so far
Nothing printed yet.
What Python does
Python reads the program from the top. Nothing has run yet.
Names
None yet.
Quick guesses
Guess first, then read why. A wrong guess is where the learning happens. Nothing here is scored.
Question 1 of 6
1.Odd or even?
print(9 % 2)0 of 6 answered
Common operator misconceptions
Each card is something beginners often believe. Tap it to see what Python really does.
Misconception 1 of 4
Notes
Everything above, in words. Open it when you want the full explanation.
Read the notesNotesOpenClose
Syntax
The general shape of the statements in this lesson.
+ - * / # add, subtract, multiply, divide (float) // % ** # floor division, remainder, power == != < > <= >= # comparisons give True or False and or not # combine bools "a" in "cat" # membership x is None # identity, for None checks (a + b) / 2 # parentheses control the order
Press Run to see what it prints.Worked examples
Read the problem, run the code, then read why it works.
Example 1 of 2
Converting minutes to hours and minutes
A film lasts 135 minutes. Show it as hours and minutes, and say whether the length in minutes is an even number.
film = 135
hours = film // 60
minutes = film % 60
print(hours, "h", minutes, "min")
print("Even length:", film % 2 == 0)Press Run to see what it prints.Predict first
How it works(open after your guess)
Common pitfalls
Mistakes people make with this topic, and what to write instead.
Pitfall 1 of 3
Using = instead of ==
Spot the bug
Can you see what is wrong? Run it to watch it go wrong, then open the fix.
score = 10 print(score = 10)
Press Run to see what it prints.Show what goes wrong and the fixWhat goes wrong and the fix
In the real world
Where this shows up in software people use every day.
- Farming
Packing eggs into boxes
A small farm collects eggs every morning and packs them into boxes of six. The farmer wants to know how many full boxes there are and how many eggs are left for the kitchen.
Why Python fits
Floor division and remainder answer the two packing questions in one line each, and a comparison turns the result into a yes or no decision.
PythonEGGS_PER_BOX = 6 collected = 83 full_boxes = collected // EGGS_PER_BOX spare = collected % EGGS_PER_BOX print("Full boxes:", full_boxes) print("Spare eggs:", spare) print("Enough for market:", full_boxes >= 12)OutputPress Run to see what it prints.
Key takeaways
Tick each one you could explain to a friend.
0 of 5
Common questions
Guess the answer first, then open it.
What does // do with negative numbers?
Why is 0.1 + 0.2 == 0.3 False?
Should I use and or the & symbol?
Your notes
Sign in to keep private notes alongside this lesson.
Where to go next
The quiz and the Lab live on their own pages, so this lesson stays about learning.
Finished this lesson?
Create an ID or sign in to save your progress across devices.
Check what you learned
12 questions, with an explanation after every answer. A verified attempt counts towards opening the module Lab.
Keep going in the Lab
7 tasks on this lesson, from writing code to tracing it by hand. Practice only, no XP.
Spaced review
Questions you miss in a quiz come back on a spaced schedule, so what you learned here sticks.
This lesson at a glance
The 7 lines of code this lesson uses, each with a short note. Keep it open while you do the quiz and the Lab.
What else you can search for and explore
Curious to go further? Each card opens a web search in a new tab.
References
Look things up here whenever you need the exact details.