Starter quiz
- Which of the following Python expressions evaluates to `True`?
- `10 > 5` ✓
- `apple` == `banana`
- `3 + 2 < 3 - 2`
- `10 <= 5`
-
- Which Python code is used to create a block of code that will run based on whether something is True or False?
- `print`
- `else`
- `if` ✓
- `input`
-
- What is the output when the program is run? ```score = 7 if score > 5: print("score is greater than 5") else: print("score is not greater than 5")```
- score is greater than 5 ✓
- score is not greater than 5
-
- Which logical operator in Python checks if two conditions are both True?
- `not`
- `or`
- `and` ✓
- `=`
-
- What will be the output of this Python code? ```is_raining = True is_cold = False if is_raining or is_cold: print("Stay indoors") else: print("You can go outside!")```
- You can go outside!
- Stay indoors. ✓
- An error message.
- No output will be produced.
-
- Arrange the following components of an if-else statement in the correct order.
- 1⇔`if`
- 2⇔condition:
- 3⇔code to execute if the condition is True
- 4⇔`else:`
- 5⇔code to execute if the condition is False
Exit quiz
- Which of the following statements accurately describes multi-path selection in Python?
- You can have more than two options for your code to choose from. ✓
- The computer checks each condition in order. ✓
- All the code inside every `if`, `elif`, and `else` block runs.
- It helps your program make different choices based on what's True. ✓
-
- In Python, what is used to introduce an additional condition in a multi-path selection statement after an initial if statement?
- `if`
- `elif` ✓
- `while`
- `==`
-
- What will be the output of the following Python code? ```grade = 75 if grade >= 90: print("A") elif grade >= 80: print("B") elif grade >= 70: print("C") else: print("Fail")```
- A
- B
- C ✓
- Fail
-
- What is the primary purpose of indentation in Python code, especially in the context of `if`, `elif`, and `else` statements?
- to make the code look visually appealing.
- to indicate which lines of code belong to which block or path of the selection. ✓
- to specify the order in which the conditions should be evaluated.
- to define the data types of variables used in the conditions.
-
- Which of the following is correct when describing nested if statements?
- The first condition needs to be True before the next condition can be run. ✓
- The first condition needs to be False before the next condition can be run.
-
- A game gives points based on a dice roll: Roll 1 or 2: lose a point. Roll 3, 4, or 5: no change. Roll 6: win a point. Which Python code is best for this?
- single `if`
- `if-else`
- `if-elif-else` ✓
-
Worksheet
Loading worksheet ...
Presentation
Loading presentation ...
Video
Lesson Details
Key learning points
- Multi-path selection checks successive conditions and selects one out of multiple paths to follow.
- Selection statements with conditions can be nested inside each other.
- If selection statements are nested then code blocks must be indented.
Common misconception
An if statement can only have two options.
An if statement can be paired with elif and else statements to create multi-path selection. This allows for multiple paths to follow in a program.
Keywords
Condition - an expression that evaluates to True or False
Multi-path selection - selection when there is more than one path for a program to follow
Nested - a block of code where a loop is contained within another loop
+