Selection
Objectives
- To understanding selection in algorithms and code
- Define the purpose of selection (conditional statements) in programming.
- Learn the syntax of
if
,elif
, andelse
statements in Python - List and explain the relational operators used in conditions (e.g., ==, !=, >, <, >=, <=).
- Review examples of Conditional Statements
- Define the role of the
elif
statement in handling multiple conditions. - Differentiate the use of
elif
fromif
andelse
.
Introduction
As part of our algorithm we will often encounter a moment when a decision needs to be taken, and depending on the outcome of that decision, either True
or False
, execute a different statement or set of statements. In Python, we use if
, elif
(else if), and else
statements to implement selection. We also rely on the relational operators covered earlier i.e. ==
, !=
, >
etc..
Example 6
Predict what the output of the program will be for these inputs:
- "What will the program print if the temperature is -5?"
- "What will the program print if the temperature is 15?"
- "What will the program print if the temperature is 25?"
- "What will the program print if the temperature is 35?"
Run the program in a Python environment and compare the actual output with your predictions.
1. The if
Statement
The if
statement allows you to execute a block of code only if a specified condition is true.
2. The elif
Statement
The elif
statement is used when you have multiple conditions to check. It is executed if the preceding if
or elif
conditions are false and its own condition is true.
Note
It makes sense to read elif
as "else if"
3. The else
Statement
The else
statement is used as a fallback option if none of the preceding conditions are true.
4. Conditions
The condition will always be some kind of Boolean expression that evaluates to either True
or False
and will use the relational operators we saw earlier:
-
Equal to (
==
): Checks if two values are equal. -
Not equal to (
!=
): Checks if two values are not equal. -
Greater than (
>
): Checks if one value is greater than another. -
Less than (
<
): Checks if one value is less than another. -
Greater than or equal to (
>=
): Checks if one value is greater than or equal to another. -
Less than or equal to (
<=
): Checks if one value is less than or equal to another.
These conditions can be further combined using and
, or
and not
to create complex conditional expressions.
Further Examples
Example 1: Basic if
Statement
Example 2: if-elif-else
Statement
Activity
Using the following program, make the changes suggested:
- Add a new condition for temperatures above 35 degrees.
- Change the humidity thresholds and observe the different outputs.
Climate Quest Project
Throughout this topic we'll be working on a large scale project: Climate Quest. In this project a player embarks on a journey to combat the effects of climate change by making decisions that impact the environment. Each choice affects the outcome of the game, emphasizing the importance of individual actions in addressing climate change.
Summary
Cheat sheet for selection (and iteration)1
Questions
Programming Tasks
For each of the following tasks write a program using Python.
Write a Python program that takes a test score as input and prints the corresponding letter grade ('A', 'B', 'C', 'D', or 'F') based on the following grading scale A: 90 or above, B: 80-89, C: 70-79, D: 60-69, F: Below 60
Write a Python program that checks if a given year is a leap year. A leap year is divisible by 4 but not divisible by 100 unless it is divisible by 400. Print 'Leap year' or 'Not a leap year' accordingly. Use the following pseudocode to help with the logic:
Write a Python program that lets a user play the game of Rock, Paper, Scissors against the computer. Implement the game logic and provide instructions for the user to input their choice.
Write a Python program to calculate the Body Mass Index BMI or a user given a height and a weight. The formula to use: bmi = weight / (height ** 2). Output the result 'Underweight' if the BMI < 18.5; 'Obese' if the BMI is > 30.5; 'Normal Weight' if the BMI is between 18.5 and < 25; 'Overweight' if the BMI is between 25 and 30.
-
[https://ehmatthes.github.io/pcc_3e/cheat_sheets/(https://ehmatthes.github.io/pcc_3e/cheat_sheets/)] ↩