Skip to content
Learnearn.uk » Python Unit Home » Selection – if..elif..else

Selection – if..elif..else

Introducing if statements

if statement flow chart (1)

If Statements

if some_test: # Notice the colon at the some_test

    do_something #Notice the 4 spaces before the do_something code

Sometimes in our programs we will want to do a certain thing only if a a certain thing happens. In this case we will usually want to use an if statement.

 

 

 

Testing equivalence

if-statement-example-flowchart

Example 1 – Testing equivalence

Quite often in our programs we will want to test if something equals something else. In order to this in python we use the == (double equals) sign.

Indented vs non-indented code.

The program executes(runs) the if statement, and then if the result of the if statement is true, it executes the indented code (Indented means you put 4 spaces before the code).

Here is another example.  Can you see difference between the indented and non-indented code?

Testing greater / less than

Example 2 – Testing  greater / less than

Another common use of an if statement is to test is a value is greater or less than an amount. For this, just like in maths, we use the > (greater than) or < (less than signs.

Comparison Operators we can use

comparison operators

Testing membership

Testing Membership

We can also use if statements to test see if inputs or variables are members of lists.

Multiple tests

Multiple Tests

Sometimes we want to do more than one test. We can do this using the OR statement
Try it out

We can also test multiple variables at the same time using either an OR statement or an AND statement.

Multiple tests with multiple variables

username = input("Please enter your username:")
password = input("Please enter your password:")
if username == "aladin" and password == "abracadabra":
    print("You can enter the cave of many shiny treasures")
if else python flowchart

If…Else… Statements

Although if statements are quite useful just on their own, we often want to force the program down two exclusive paths. In other words the program must execute(run) one of the two branches. For the we use a combination of if…else..

 

 

 

 

 

 

Introducing if..elif..else

if..elif...else decision flowchart python

Sometimes we might need to choose between a number of different exclusive paths. For this it is best to use and if…elif…else… chain, because then we know that it will only ever choose one path.

 

 

 

 

 

Demo

Challenge 13 - Advice Please

ch13

Challenge 14 - Darts

ch14

Challenge 15 - Charity Collection

ch15

Challenge 16 - Area of a Rectangle

ch16

Challenge 17 - Advice Please(Part 2)

ch17

Challenge 18 - Tyre and Brake Wear

ch18

Challenge 19 - Solid, Liquid, Gas

ch19

Challenge 20 - Charity Collection ( Part 2)

ch20

Attribution : Please note these challenges were created by: G. Reid – Buckhaven High School

Kahoot Review Quiz

 

Let's learn Python YouTube Video -Selection

Let’s learn Python YouTube Video -Selection