Lesson Objective
- Understand Selection and how to use IF Statements in Python programming.
- Understand the meaning of the mathematical, comparison and logical operations and know how to use them.
KS3, GCSE, A-Level Computing Resources
If statements are used to make decisions in your programs. Different parts of your code will run depending on whether or not a condition is true or false.
name = input(“Enter your name: ”) print (“Your name is...”) print (name) if name == “Mr Ahmed”: print (“Welcome Mr Ahmed. What would you like to do...”) else: print (“Invalid User!! Goodbye.”)
Enter your name: Susan Your name is... Susan Invalid User!! Goodbye. ------ Enter your name: Mr Simpson Your name is... Mr Simpson Invalid User!! Goodbye. ------ Enter your name: Mr Ahmed Your name is... Mr Ahmed Welcome Mr Ahmed. What would you like to do...
When you need to check multiple conditions, you could use an "elif" statement. This allows you to execute a block of code as soon as a condition is TRUE. The example code now allows more names to be accepted into the program.
name = input(“Enter your name: ”) print (“Your name is...”) print (name) if name == “Mr Ahmed”: print (“Welcome Mr Ahmed. What would you like to do...”) elif name == “Melon Lord”: print (“Welcome Melon Lord!! What would you like to do...”) elif name == “Daenerys”: print (“Daenerys Stormborn of House Targaryen, the First of Her Name, Queen of the Andals and the First Men, Protector of the Seven Kingdoms, the Mother of Dragons, the Khaleesi of the Great Grass Sea, the Unburnt, the Breaker of Chains”) else: print (“Invalid User!! Goodbye.”)
Enter your name: Mr Ahmed Your name is... Mr Ahmed Welcome Mr Ahmed. What would you like to do... ------ Enter your name: Melon Lord Your name is... Mr Ahmed Welcome Melon Lord!! What would you like to do... ------ Enter your name: Daenerys Your name is... Daenerys Daenerys Stormborn of House Targaryen, the First of Her Name, Queen of the Andals and the First Men, Protector of the Seven Kingdoms, the Mother of Dragons, the Khaleesi of the Great Grass Sea, the Unburnt, the Breaker of Chains ------ Enter your name: Daisy Your name is... Daisy Invalid User!! Goodbye.
IF Statements inside another IF Statement structures are referred to as nested IF Statements.
question = input("Do you like Penguins?: ") if question == "Yes": subquestion = input("Can Penguins fly?: ") if subquestion == "Yes": print("Awesome, You know things.You must really like Penguin!!") else: print("Are you really sure you actually like Penguins?") else: print("Oh OK... Bye...")
Do you like Penguins?: No oh OK... Bye... ------ Do you like Penguins?: Yes Can Penguins fly?: No Are you really sure you actually like Penguins? ------ Do you like Penguins?: Yes Can Penguins fly?: Yes Awesome, You know things.You must really like Penguin!!
These operators are used to carry out arithmetic calculations using variables and other data sets.
Example of + Operator being used below.
fruit = 40
vegetable = 10
answer = fruit + vegetable
print(answer)
50
Example of - Operator being used below.
fruit = 40
vegetable = 10
answer = fruit - vegetable
print(answer)
30
Example of ÷ Operator being used below.
fruit = 40
vegetable = 10
answer = fruit / vegetable
print(answer)
4.0
Example of * Operator being used below.
fruit = 40
vegetable = 10
answer = fruit * vegetable
print(answer)
400
Example of Remainders Division (Modulo) being used below.
fruit = 40
vegetable = 10
answer = fruit % vegetable
print(answer)
0
Example of Whole Number Division (Floor) being used below.
fruit = 40
vegetable = 10
answer = fruit // vegetable
print(answer)
4
These operators are used to compare variables and other data types.
Example of < operator being used below.
fruit = 40 vegetable = 10 answer = fruit * vegetable print (answer) if answer < 500: print ("Elephant") else: print ("Mouse")
400 Elephant
Example of > operator being used below.
fruit = 40 vegetable = 10 answer = fruit * vegetable print (answer) if answer > 500: print ("Elephant") else: print ("Mouse")
400 Mouse
Example of == operator being used below.
fruit = 40 vegetable = 10 answer = fruit * vegetable print (answer) if answer == 500: print ("Elephant") else: print ("Mouse")
400 Mouse
Example of != operator being used below.
fruit = 40 vegetable = 10 answer = fruit * vegetable print (answer) if answer != 500: print ("Elephant") else: print ("Mouse")
400 Elephant
Example of >= operator being used below.
fruit = 40 vegetable = 10 answer = fruit * vegetable print (answer) if answer >= 500: print ("Elephant") else: print ("Mouse")
400 Mouse
Example of <= operator being used below.
fruit = 40 vegetable = 10 answer = fruit * vegetable print (answer) if answer <= 500: print ("Elephant") else: print ("Mouse")
400 Elephant
These operators are used to compare variables and other conditions to determine is a statement is either TRUE or FALSE.
Example of AND operator being used below.
fruit = 40 vegetable = 10 answer = fruit * vegetable print (answer) name = input("Enter your name: ") if answer == 400 and name == "Mr Ahmed": print ("Elephant") else: print ("Mouse")
400 Enter your name: Mr Ahmed Elephant ------ 400 Enter your name: Mr Simpson Mouse
Example of AND operator being used below.
fruit = 40 vegetable = 5 answer = fruit * vegetable print (answer) name = input("Enter your name: ") if answer == 400 or name == "Mr Ahmed": print ("Elephant") else: print ("Mouse")
200 Enter your name: Rahaid Mouse ------ 200 Enter your name: Mr Ahmed Elephant