Learning Hub/Python/Session 2

Session 2 of 5 · 60 minutes

Making Decisions

Compare values and make your program choose what to do with if, elif and else.

Goals

By the end of this session you can:

  • Compare values with ==, !=, <, >, <= and >=
  • Write if, elif and else blocks and indent them correctly
  • Combine conditions with and, or and not
  • Use % to check whether a number is even or odd

Words to know

condition
A question with a True or False answer, like age >= 13.
boolean
A value that is either True or False.
indentation
The spaces at the start of a line. In Python they show which lines belong inside an if.
modulo (%)
The remainder after dividing. 7 % 2 is 1.

The lesson

Step 1: Asking yes/no questions

A comparison asks a question about two values. The answer is always True or False.

Python
print(7 > 3)
print(7 == 3)
print(7 != 3)
print(5 <= 5)
Output
True
False
True
True
SymbolMeaning
==is equal to
!=is not equal to
< and >less than, greater than
<= and >=less than or equal, greater than or equal

Be careful: = stores a value, and == compares two values. They are not the same.

Step 2: if and else

An if runs some code only when its condition is True. else runs when it is not.

Python
temperature = 31

if temperature > 30:
    print("It's hot. Drink water!")
else:
    print("Nice weather.")
Output
It's hot. Drink water!

Two things to notice:

  1. The if line ends with a colon (:).
  2. The lines that belong inside are indented by 4 spaces. That is how Python knows where the block starts and ends.

Step 3: More than two choices with elif

elif means “else if”. Python checks the conditions from top to bottom and runs the first one that is true.

Python
score = 82

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
else:
    print("Keep practicing!")
Output
Grade: B

Why the order matters: 82 is also greater than 70, but Python stops at the first true condition. If you wrote the >= 70 check first, everyone above 70 would get a C.

Step 4: and, or, not, and the remainder

Combine conditions with and, or and not.

Python
age = 15
has_ticket = True

if age >= 13 and has_ticket:
    print("Come in!")

if age < 5 or age > 100:
    print("Please ask a grown-up.")

print(not has_ticket)
Output
Come in!
False
  • and needs both sides to be true.
  • or needs at least one side to be true.
  • not flips True to False and False to True.

The % operator gives the remainder of a division. It is the standard way to test for even numbers.

Python
print(7 % 2)
print(8 % 2)
Output
1
0

If number % 2 == 0, the number divides evenly by 2, so it is even.

Step 5: Putting it together

Here is a short program that makes a decision from the user’s input.

Python
age = int(input("How old are you? "))

if age >= 18:
    print("You can vote in many countries.")
elif age >= 13:
    print("You're a teenager.")
else:
    print("You're still a kid. Enjoy it!")
Output
How old are you? 15
You're a teenager.

Tip: When your program does the wrong thing, test it with the “edge” values: the numbers right on either side of each rule, like 12 and 13.

Exercise

Even or odd?

  1. Ask the user for a whole number and convert it with int().
  2. Use % to check whether the number divides by 2 with no remainder.
  3. Print even or odd accordingly.
  4. Extra: also print That's a big number! if the number is bigger than 100.
Need a hint?

A number is even when number % 2 == 0.

Small project

Ticket price calculator

A theater charges by age. Write a program that asks for the visitor's age and prints their ticket price.

  1. Ages 0 to 4 get in free.
  2. Ages 5 to 12 pay $5.
  3. Ages 13 to 64 pay $10.
  4. Ages 65 and over pay $6.
  5. Print a friendly message such as Your ticket costs $5. If the age is below 0, print That doesn't look like a real age.

Stretch it: Ask whether the visitor has a student card and give a $2 discount to anyone aged 13 to 64 who answers yes.

Quiz

Check your understanding

Pick one answer for each question. Your score appears right in the page; nothing is sent anywhere.

  1. Question 1What is the value of 7 > 3 ?
  2. Question 2Which symbol checks whether two values are equal?
  3. Question 3What does this print?x = 4 if x > 5: print("big") else: print("small")
  4. Question 4What is 10 % 3 ?
  5. Question 5Which condition is True only when age is between 13 and 19 inclusive?