Learn / Python Level 1

Lesson 4: Making decisions

By the end you can: Use if, elif and else so your program can choose what to do.

1Recall

Question 1

1. What is 9 % 2?

Question 2

2. What does int("12") give?

Question 3

3. What does "3" + "4" give in Python?

2New idea

Programs often need to make choices: if it's raining, take an umbrella; otherwise, take sunglasses. In Python we use if and else.

  • == means "is equal to" (one = is for putting values in variables)
  • Other comparisons: != not equal, < less than, > more than, <= and >=
  • The line after if ends with a colon :
  • The lines that belong to the if are indented (4 spaces)
  • elif means "else, if": a second question to check

3Show me

Try 5, 20 and 30.

4Challenge 1

5Challenge 2

6Check

Question 1

1. Which symbol checks if two things are equal?

Question 2

2. What must come at the end of an if line?

Question 3

3. If score is 7, which runs? if score > 10: print("A") elif score > 5: print("B") else: print("C")

7At home

8Reflect

  • Name a decision a game makes using if.
  • Why does the order of if and elif matter in Medal maker?