Selection
Selection is where the next code to run, if any, is determined by the result of a Boolean condition i.e. a condition that can be evaluated as true or false. The main method for doing selection in Python is the IF statement but more recently they have also brought a case select method to Python.
Logical operators
There are three logical operators that can be used to create more complex Boolean conditions.
Logical operator |
What it does |
Example |
AND |
Both conditions joined by an AND operator must be true for the whole condition to be true |
if age >18 and age < 30: This would be true only if the age entered is over 18 and under 30 |
OR |
If either condition joined by an OR operator is true then the whole condition is true |
if age >18 or age < 30: This would always be true because if the age entered is over 18 even if it is not under 30 and it would be true for any value under 30 regardless of whether it is over 18 |
NOT |
Not reverses the output of a Boolean condition |
if not(name == “Bob”): This would evaluate as true for all names aside from Bob. |
Basic if
At the most basic level we can have an if statement, that checks some code and either runs a piece of code if the condition is true, or simply does nothing if it is false and goes on to the next question.
name = input(“Enter your name: “) if name == “Bob”: print(“Hi Bob. My name is Bob too.”) |
Basic if syntax rules:
- If is in lowercase
- There is a Boolean condition
- There is a colon on the end
- The code to be run if it is true is indented
Syntax checker
Fix the lines below so the if statement works as expected.
name = input("Enter your name: ") |
if name = "Bob": |
print("Bob is my name too") |
If else
An if-else unlike an if always runs some code. The code indented under the if condition if the condition is true and the code indented under the else condition if it is false.
name = input(“Enter your name: “) if name == “Bob”: print(“Hi Bob. My name is Bob too.”) else: print(f”Hi {name}”) |
If else syntax rules:
- If is in lowercase
- There is a Boolean condition
- There is a colon on the end
- The code to be run if it is true is indented
- Else is in lowercase and not indented
- Else has not attached condition
- There is a colon after else
- The code to be run if the original condition is false is indented under the else statement
name = input("What is your name? ") |
if name = "Bob": |
print("Bob is my name too") |
else: |
print(f"Hi {name}") |
If elif else
Elif is short for else if. This command lets us check more conditions. If the first if evaluates as false then we check the second condition i.e. the first elif condition. We can have as many elif conditions as we want. It is important to note that as soon as any condition evaluates as true the IF statement is resolved and no more conditions are checked. We can choose to have an else attached or not. If we have an else when we have elif conditions then it will run only if all conditions are false.
name = input(“Enter your name: “) if name == “Bob”: print(“Hi Bob. My name is Bob too.”) elif name == “Sam”: print(“Hi Sam. My brother is called Sam too.”) elif name == “Jim”: print(“Hi Jim. My dad is called Jim too.”) else: print(f”Hi {name}”) |
If else syntax rules:
- If is in lowercase
- There is a Boolean condition
- There is a colon on the end
- The code to be run if it is true is indented
- Any elif condition has a Boolean condition attached
- Elif is in lowercase and not indented
- The code to be run if the elif condition is true is indented
- Else is in lowercase and not indented
- Else has not attached condition
- There is a colon after else
- The code to be run if the original condition is false is indented under the else statement
name = input("What is your name? ") |
if name = "Bob": |
print("Bob is my name too") |
elif name = "Joe": |
print("My Dad is called Joe too") |
else: |
print(f"Hi {name}") |
Case select
A case select statement is a fairly new technique in python. We pass a variable to the match function and then define what to do in the case of each possible input they may have entered. The expression case _ is known as the base case and is effectively the same as using else.
lang = input("What's the programming language you want to learn? ") match lang: case "JavaScript": print("You can become a web developer.") case "Python": print("You can become a Data Scientist") case "PHP": print("You can become a backend developer") case "Java": print("You can become a mobile app developer") case _: print("The language doesn't matter, solving problems matters.")
|
Fix the code below so the match case statement works as expected.
name = input("What is your name? ") |
match name: |
case "Bob": |
print("My name is Bob too") |
case "Joe": |
print("Joe is my Dad's name too") |
case "Sue": |
print("Sue is my Mum's name too") |
case _: |
print(f"Hello {name}") |
You can also use this method to check things are within a range as seen below.
number = int(input("num: ")) match number: case num if 1 <= num < 13: print("Child") case num if 13 <= num < 20: print("Teen") case _: print("Adult")
|
Fix the code below so the match case statement awards 1 ticket for a score of less than 5, 3 tickets for a score between 5 and 9 and 5 tickets for a score of 10 or more.
score = int(input("What is your name? ")) |
match score: |
case score if score < 5: |
print("You win 1 ticket") |
case score if score < 10: |
print("You win 3 tickets") |
case _: |
print("You win 5 tickets") |
Case select syntax rules:
- Use the keyword match followed by a variable that’s value will be checked
- There is a colon on the end of the match line
- Cases are indented one level from the match line
- Use the keyword case to specify specific cases you want to handle differently
- If needed, add criteria to your cases
- There is a colon after the case is specified
- What happens in the event a case is matched is indented under the case description
- If you want a base case declare case _