CurriculumWeek 01 · FoundationsOperators and decisions
Lesson 01.02 · ~25 min read

Operators and decisions

Arithmetic, comparisons, and logical operators. Then if / elif / else — the smallest unit of programmer judgement.

By Kelvin AmoabaUpdated May 12, 2026Prerequisite: 01.01

What we're doing today

By the end of this session, you will have written a program that looks at something — a number, a price, an age — and decides what to do next. Up to now, your programs have only been able to say things. Today they learn to think. You give the computer a question with a yes-or-no answer, and it picks one path or another based on the reply.

Every useful program is a tree of small choices like that. Should I show a discount? Should I let this person in? Should I warn the user that their cart is too expensive? By the end of the hour, you will know how to write all of those.

Doing maths in Python

Before we get to decisions, we need a little maths, because most decisions are about numbers. Python uses the same symbols you saw on the back of your primary school exercise book:

print(3 + 4)      # 7
print(10 - 5)     # 5
print(6 * 7)      # 42
print(20 / 3)     # 6.666...

The star * is multiplication. The forward slash / is division. Nothing surprising so far. When I say operator, I just mean one of these symbols that does something to the numbers around it. + is an operator. * is an operator. That is the whole word.

Two more you will need that are not on the back of the exercise book:

print(20 // 3)    # 6   — division that throws away the decimal
print(20 % 3)     # 2   — only the remainder, nothing else

The double slash // is called integer division, which is a fancy way of saying "divide, then drop everything after the decimal point." 20 / 3 is 6.666..., but 20 // 3 is just 6. It is what you would do in your head when you don't care about the leftover bit.

The percent sign % is called modulo, and it is the one beginners always squint at. The simplest way to think about it: imagine you have 20 oranges and you want to put them into bags of 3. You will fill 6 bags. You will have 2 oranges left over that don't fit in a full bag. That leftover — the remainder — is what % gives you. So 20 % 3 is 2. If you were sharing 20 cedis equally between 3 friends, each one gets 6 cedis and 2 cedis are left in your hand. That 2 is the modulo.

You will use % more than you expect. Asking "is this number even?" is just number % 2 == 0. Asking "does this divide evenly?" is the same idea.

Asking yes-or-no questions

Now we get to the real point of the lesson. A comparison operator is a symbol you put between two things to ask a question that has a yes-or-no answer. "Is this bigger than that?" "Are these two the same?" Python looks at the two sides, decides yes or no, and hands you back the answer.

A yes-or-no answer in Python is called a boolean. It is one of only two words: True or False, capital letter at the front, no quotes. That is it. Booleans are simple on purpose — that is why they are useful.

Here is a comparison in action:

price = 80
print(price > 100)    # False
print(price < 100)    # True
print(price == 80)    # True

Six comparisons cover almost everything you will ever need:

SymbolWhat you are asking
==are these two things the same?
!=are these two things different?
>is the left side bigger?
<is the left side smaller?
>=is the left side bigger or equal?
<=is the left side smaller or equal?

Now I have to warn you about the one mistake every single person in this class will make in the next two weeks. Look closely:

  • = (one equals sign) stores something in a labelled jar. We did this last session: name = "Amara".
  • == (two equals signs) asks a question. "Are these two the same?"

One sets. Two asks. If you write if price = 100, Python will yell at you, because it has no idea what you wanted. Did you mean to store 100 in price? Did you mean to check if price is 100? It refuses to guess. Every Python programmer I know — me included — has been caught by this. When you see your first SyntaxError next week, this is the first thing to check.

Combining questions

A single question is fine, but real life is not made of single questions. Should you carry an umbrella today? You probably ask two things at once: "Is the sky grey?" and "Am I going out for more than ten minutes?" If both are yes, you grab the umbrella. If either one is no, you leave it at home.

Python has three small words for combining yes-or-no questions: and, or, not. These are called logical operators — a fancy name for "the words that glue yes-or-no questions together."

sky_is_grey = True
going_out_long = True
 
print(sky_is_grey and going_out_long)   # True
print(sky_is_grey or going_out_long)    # True
print(not sky_is_grey)                  # False

Three rules. Memorise them:

  • and is only True when both sides are True. If either side is False, the whole thing is False. Like a doorman at a club who needs to see two things — your ID and your ticket. Miss one, you don't get in.
  • or is True when at least one side is True. The doorman only needs to see your ID or your name on the guest list. Either is enough.
  • not flips the answer. not True becomes False. not False becomes True. It is the word "no" in front of a sentence.

When you start mixing them in one line, wrap parts in round brackets so you can read it back the next day:

(age >= 18 and has_id) or is_on_guest_list

That reads almost like English: "they're old enough and they have ID, OR their name is on the list." Either path lets them in.

Making the decision: if, elif, else

This is the big one. Take everything above — comparisons, booleans, logical operators — and feed the answer into an if statement. An if is how Python picks one path or another.

Picture a paper flowchart you would sketch in your notebook: an arrow comes in, hits a diamond that says "Is it raining?", and splits into two arrows — yes goes one way, no goes the other. An if is that diamond, written in code.

Here is the shape:

price = float(input("How much is it? "))
 
if price > 100:
    print("That's expensive. Think twice.")
elif price > 50:
    print("Reasonable. Go for it.")
else:
    print("That's a steal.")

Run that cell. Try 120. Then 70. Then 20. Only one of the three lines prints each time, because Python picks one branch and skips the rest.

Four things to notice about how this is written, because every one of them is a place beginners trip up:

  • Every if, elif, and else line ends with a colon :. Forget the colon, you get a SyntaxError. It happens to all of us.
  • The line underneath is indented — pushed in by four spaces. The indented block is what runs when the question above is True. If the line is not indented, Python will not connect it to the if and will get confused.
  • Python checks the questions top to bottom and stops at the first one that answers True. The rest are ignored. So order matters. If you put elif price > 50 above if price > 100, then a price of 120 would match > 50 first and you would never reach the "expensive" branch.
  • elif is short for "else if" — it means "if the one above was false, try this one." You can stack as many elif lines as you want. else at the bottom is optional; it is the catch-all for "none of the above matched."

Why the spaces matter

In some other programming languages, you wrap a block of code in curly braces { }. Python does not. In Python, the indentation is the block. Those four spaces are not for looking pretty — they are how Python knows where the if ends and the rest of your program begins.

Pick four spaces. Stick with four spaces. Do not mix tabs and spaces in the same file. In Colab, the Tab key already gives you four spaces. If Python ever complains with IndentationError, look for a line that is pushed in by the wrong amount.

Putting it all together

Here is a small program that uses comparisons, a logical operator, and an if chain. It decides whether you should carry an umbrella.

chance_of_rain = int(input("Chance of rain (0-100)? "))
going_out_long = input("Out for more than 10 min? (yes/no) ") == "yes"
 
if chance_of_rain >= 60 and going_out_long:
    print("Take the umbrella.")
elif chance_of_rain >= 60:
    print("Maybe a hood is enough.")
else:
    print("Leave it at home.")

Read the second line carefully. The == at the end asks "did the user type the word yes?" The result of that question — a True or a False — is what gets stored in going_out_long. So going_out_long ends up holding a boolean, not a string. This is a pattern you will use a lot: ask a yes-or-no question, store the answer, then check it later in an if.

Your exercise

Open a fresh Colab cell. Write a program that splits a bill among friends.

  1. Ask the user for the total bill in cedis. Convert it to a float.
  2. Ask the user for the number of friends. Convert it to an integer.
  3. Use integer division // to work out how much each person pays as a whole number of cedis.
  4. Use modulo % to work out how many cedis are left over that don't split evenly.
  5. Print the per-person amount and the leftover.
  6. If the leftover is not zero, print an extra line saying "Someone has to cover the extra X cedis."

Example run:

Total bill: 100
Number of friends: 3
Each person pays: 33 cedis
Leftover: 1 cedis
Someone has to cover the extra 1 cedis.

If you finish early, add an age check at the top: ask for the user's age, and only run the calculator if they are 18 or older. Otherwise print a polite "come back later."

Common slip-ups

These are the mistakes I see every single term. When you hit one, you will recognise it.

  • Writing = instead of == inside an if. One stores. Two asks. Python yells when you mix them up.
  • Forgetting the colon at the end of if, elif, or else. The error message is SyntaxError: expected ':'. Read your line, add the colon, move on.
  • Wrong indentation. Four spaces, always. Do not mix tabs and spaces. If Python says IndentationError, the fix is almost always staring you in the face on the line above.
  • Putting the wider condition first. if price > 50 placed above if price > 100 will catch every expensive price before the "expensive" branch ever gets a chance. Order your if / elif from most specific to least specific.
  • Mixing and and or without brackets. When the line gets long, wrap groups in round brackets. Your future self will thank you.

That is the whole session. Run the bill-splitter, take a screenshot of it working, and bring questions to our next live session. From here, every program you write is going to use if.

Resources

★ recommended