🎯 Learning Objectives
Develop the Programming and Development, Algorithms Learning Strands:
- Use relational operators to form logical expressions
- Use binary selection (if, else statements) to control the flow of program execution
- Generate and use random integers
💬 Key Vocabulary
- Selection
- relational operators,
- logical (or Boolean) expressions
- conditions
- randomness
- execution
- walk-through
🔎 Crunching Numbers in Python
Last lesson, you…
- Used arithmetic expressions to calculate values
- Used variables to store and reference values
- Followed walk-throughs of code and kept track of variable values
- Wrote programs that receive numerical input from the keyboard
In this lesson, you will…
- Use selection (if statements) to control the flow of program execution between branches
- Introduce elements of randomness into your programs
📖 Selection in Python
![](https://i0.wp.com/bournetocode.com/wp-content/uploads/2020/08/image-16.png?w=600&ssl=1)
- Selection is when your programs check conditions and select the path of action that they will follow accordingly.
![](https://i0.wp.com/bournetocode.com/wp-content/uploads/2020/08/image-17.png?w=600&ssl=1)
You will need an if
or an if, else
when there is more than one possible path for your program to follow.
📝 Worksheet – Practise using selection
- Download this lesson’s worksheet to your Home Drive.
📖 Selection example
- The condition will check if the value of
user
is equal to the string"Elizabeth"
. - The expression
user == "Elizabeth"
will evaluate to eitherTrue
orFalse
. - This is the
if
block, i.e. the code that will be executed if the condition isTrue
. - This is the
else
block, i.e. the code that will be executed if the condition isFalse
. - Only one of these blocks will be executed, depending on the value of the condition.
![](https://i0.wp.com/bournetocode.com/wp-content/uploads/2020/08/image-18.png?resize=467%2C199&ssl=1)
📖 Selection – Syntax Pitfalls
- It’s
if
andelse
. No capitals. - A colon : is always required after the
if
condition and afterelse
. - Use indentation to indicate which statements ‘belong’ to the
if
block and theelse
block. - The
==
operator checks for equality. The single=
is only used in assignments. user
is a variable. Don’t use quotes."Elizabeth"
is a string literal. It needs quotes.
![](https://i0.wp.com/bournetocode.com/wp-content/uploads/2020/08/image-19.png?resize=467%2C124&ssl=1)
📖 Relational operators in Python (comparisons)
You can use these operators to compare the values of expressions.
Symbol | Meaning | Examples | Meaning |
---|---|---|---|
== | equal to | a == 1 | Does a equal 1? |
!= | not equal to | b != c | Are b and c different? |
< | less than | d < 3 | Is d less than 3? |
<= | less than or equal to | d <= 3 | Is d at most 3? |
> | greater than | d > 10 | Is d greater than 10? |
>= | greater than or equal to | d >= 10 | Is d at least 10? |
- Expressions formed using these operators evaluate to either
True
orFalse
. - You can also use these operators to compare alphanumeric values (strings).
📝 Activity 2 – Task 1 – Film Critic
- You are going to make a program that asks for the user’s favourite film. The program will either react enthusiastically to one particular film or display a generic comment.
- Follow the instructions for Task 1 on your worksheet and complete the code on the trinket.io assignment to complete this task.
📝 Activity 2 – Task 2 – Lucky Number
- This program picks a specific ‘lucky number’ and displays it to the user.
- Follow the instructions for Task 2 on your worksheet to complete the code below.
- Use the trinket.io assignment to complete this task.
In this lesson, you…
- Used selection (if statements) to control the flow of program execution
- Introduced elements of randomness into your programs
Next lesson, you will…
- Explore how selection can handle more than two possible branches
- Introduce iteration (while statements) to allow the flow of program execution to include loops
🏅 Badge it
🥈 Silver Badge
- Complete the Silver Task 1 – Film Critic and upload the worksheet to bournetolearn.com.
🥇 Gold Badge
- Complete the Gold Task 2 – Lucky Number tasks in the worksheet.
🥉 Platinum Badge
- Complete the Platinum Task – Eligible to Vote in the worksheet.