Week 3 of 105 to 6 hours5 exercises

Week 3: Making decisions

Compare values, combine conditions and choose what your program does with if, elif and else.

By the end of this week you can

  • Compare values and get True or False
  • Combine conditions with and, or and not
  • Write if, elif and else blocks
  • Understand indentation

Booleans and comparisons

A comparison asks a yes/no question and gives back a bool: True or False. The comparison operators are == (equal), != (not equal), < > (less, greater), and <= >= (less or equal, greater or equal).

Remember: one = stores a value, two == compares. Python lets you chain comparisons the way you would write them in maths, so 0 < age < 120 works as you would hope.

Each line gives True or False. You can store these results in variables too.

if, elif and else

An if statement runs a block of code only when its condition is True. The block is the group of lines indented underneath. Python uses indentation (four spaces is the convention) to know which lines belong together, so spacing is part of the grammar.

Add elif (short for else if) to test further conditions, and else for everything that is left. Python checks from the top and runs the first block whose condition is True, then skips the rest.

Change marks and run again. Only one branch ever runs.

and, or, not

and is True only when both sides are True. or is True when at least one side is True. not flips True and False.

Python stops as soon as it knows the answer (this is called short-circuiting), which is handy: in a and b, if a is False, b is never looked at.

Some values count as False when used in a condition: 0, an empty string, an empty list and None. Everything else counts as True. This lets you write if name: to mean "if the name is not empty".

An empty string counts as False, so not name is True here.

Common mistakes

  • Using = inside a condition. if age = 18: is an error; you want ==.
  • Forgetting the colon at the end of the if, elif or else line.
  • Inconsistent indentation. Pick four spaces and keep to it; do not mix tabs and spaces.
  • Writing if x == 1 or 2:. That does not mean what it looks like. Write if x == 1 or x == 2: or if x in (1, 2):.

Quick recap

  • Comparisons give True or False.
  • if / elif / else runs the first block whose condition is True.
  • and, or and not combine conditions.
  • Indentation is how Python groups lines.

Practise this week

Work through the 5 exercises for Week 3. Each one tells you which tests pass, and has a hint and a solution if you get stuck.

Open Week 3 exercises

Check your understanding

Pick an answer for each question, then press Check answers.

  1. What does print(5 == 5.0) show?
  2. Which block runs when marks = 80 in: if marks >= 90 ... elif marks >= 75 ... elif marks >= 60 ... else ...?
  3. What is True and not False?
  4. Which of these counts as False in a condition?
  5. What is wrong with: if age >= 18 print("adult")

This course is for learning. The Python engine runs in your browser and your code is never sent to us.