Lesson 4
Operators and decisions
Operators are the symbols that do something with values.
Math
+ - * /
// whole division, % remainder, ** power
Comparing
== equal, != not equal
< > <= >=
The answer is always True or False.
Combining
and both must be true
or at least one
not flips it
Comparisons are how a program decides things, with if.
Everything indented under the if only runs when the comparison is True.
if gold >= 100:
print("You are rich!")
else:
print("Keep looking.")Lesson 4 · Try it
Play with operators
Press Run, then try to:
- What is
17 % 5? Guess first, then check. - Change
goldso that "You are rich!" is printed. - Watch out:
=puts a value in a box,==asks "are they equal?". Tryif gold = 50:and read the error.
Output
Press ▶ Run to see what happens.
Lesson 4 · Exercise
Share the treasure
100 gold coins must be shared between 7 pirates. Everyone gets the same number of whole coins. How many does each get, and how many are left over for the captain?
Output
Press ▶ Run to see what happens.
💡 Need a hint?
each = coins // pirates gives whole coins. left_over = coins % pirates gives what is left.