Selection

<< Previous: Practical challenges 2Next: Practical Challenges 3 >>

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.

Comparison operators

We have a number of comparison operators we can use to make Boolean conditions in Python.

Symbol Description
== To check if one value is equal to another
!= To check if one value is not equal to another
> To check if one value is greater than another
>= To check if one value is greater than or equal to another
< To check if one value is less than another
<= To check if one value is less than or equal to another

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:

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:





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:





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:

Knowledge check


Questions:
Correct:

Question text


<< Previous: Practical challenges 2Next: Practical Challenges 3 >>

© All materials created by and copyright S.Goff