Condition: a value is checked to return True or Flase
Selection: A condition is used to see which lines of code, if any, will run.
Indentent: Spaces given to code
if statement: Runs code that is indendented when a condition is met.
elif statement: Stands for "Else If". Is used as another question if the if statement is not met.
else statement: Runs this code when the if condition is not met.
NOTE:
IF Elif and Else are known as Selection in programming. Selection uses a condition to allow branching code. This links to the Boolean lesson.
We can have many if statements one after another.
elif and else can't exist by itself, it has to have an if statement to belong to. You can also have many elif statements, but only one else.

What does the 'if' keyword do in Python?
Look at the code for each question.
What do you think will be the output?
What is the actual output?
Code in each part of if and else is INDENTED to show it belongs
Look at the code on the right.
Run it and see what it does.
Click on the pencil icon at the top to see the code again.
FIRST EXAMPLE:
Run the program and see what it does
Can you add your own elif statement on line12?
Second Example:
Run the program. What does this do? Try entering a number between 1 and 10
Try entering a word
Now try a number below 0 or a number bigger than 10. What happens?
Third Example
Run the program. Try entering xx@email.com
Change the variable for myEmail to make it easier to test.
See the rest of the comments in the code and see what happens
Code:
print("What shall I do today?")
weather = input("What is the weather like today? ")
if weather == "Sunny":
print("Time to play tennis.")
elif weather == "Rainy":
print("Time to stay inside and read a book.")
elif weather == "Snowing":
print("Time to go Skiing!")
#TODO: Add an elif statement here for "Foggy"
else:
print("Guess I don't know what to do with this weather")
Code 2:
#Run the code. Make sure you understand it before changing it.
numberToGuess = 7
#What happens if I put == here? Change the number as well!
print("One go only. Guess my number which is between between 1 and 10:")
number = int(input())
if number == numberToGuess:
print("You got it!")
elif number < numberToGuess and number >= 0:
# What does this line do? It has the word 'and'.
#What does >= mean?
print("Higher")
elif number > numberToGuess and number <= 10:
#Notice this time it is <=
print("Lower")
else:
print("Not a number")
#Enter a number that gets this to print.
Code 3:
#Run the code - When you understand the code, change some of the if conditions. Then move on to the next section
#Change the value here
myEmail = "xx@email.com"
enteredEmail = input("Enter User email: ")
if (enteredEmail.lower() == myEmail.lower()):
print("Welcome.")
#Does the code still work if you change if to elif?
if (enteredEmail == "Harry"):
print("Hagrid left a message:")
print("You are a wizard, Harry!")
newPassword = input("Your Password has expired. Enter new Password: ")
#Change the comparison here
if (len(newPassword) < 3):
print("Your Password is ridiculously short")
passwordConfirm = input("Type your password again. ")
#What happens if I change == to = ?
if (newPassword == passwordConfirm):
print("Brilliant. They match. New password saved. ")
Help:
print ( ) is used to output text
input( ) is used to get the user to type something
Variables store values that can change.
Name = Value here
https://www.w3schools.com/python/python_conditions.asp
RECAP
Boolean: a data type that can hold one of two values (true or false)
> Symbol: Greater than
< Symbol: Less than
== Symbol: Compares if both values are the same
!= Symbol: Not equal to
>= Symbol: Greater than or equal to
<= Symbol: Less than or equal to
Selection -
if (condition) :
(indent) program here....
Create a program that asks the user to enter a name.
If the name is "Harry Potter", output "The boy who lives!"
Adapt the last code so that it checks if the user has entered "Mark".
If it is Mark, output "Are you Mark Hamil! I love Star Wars! Big fan."
if it is anyone else,
output "Hello, [name]"
Create a program that asks the user to enter two numbers.
Output which number is larger.
When will 'print('no')' execute?
What is the purpose of the 'else' keyword?
What does 'a == b' check?
What do you think will be the output from the following code:
print ( 10 > 5 )
What do you think will be the output from the following code:
if 10 > 5:
print ("Ten is larger than five")
What do you think will be the output from the following code:
if 10 < 5:
print ("Ten is larger than five")
What do you think will be the output from the following code:
name = "007"
if name == "007":
print("Welcome, Agent 007.")
What do you think will be the output from the following code:
myOption = 1
if myOption == 1:
print("Game Start")
elif myOption == 2:
print("Game Load")
elif myOption == 3:
print("Credits")
What do you think will be the output from the following code:
myOption = 1
if myOption == 1:
print("Game Start")
elif myOption == 2:
print("Game Load")
elif myOption == 3:
print("Credits")
What do you think will be the output from the following code:
myAction = "Hug"
if myAction == "Hug":
print("I hugged her and congratulated on her success.")
if myAction == "Kiss":
print("We shared a kiss and enjoyed the moment...")
else:
print("Where shall we to celebrate?")
What do you think will be the output from the following code when the user enters the number 10:
print("Enter a number: ")
odd_even = int(input())
odd_even = odd_even % 2
if odd_even == 1:
print("Your number is odd")
elif odd_even == 0:
print("Your number is even")
What do you think will be the output from the following code when the user inputs "Foggy":
print("What shall I do today?")
weather = input("What is the weather like today? ")
if weather == "Sunny":
print("Time to play tennis.")
else weather == "Raining":
print("Read a book")
Create a program that asks the user to make a new password.
If the length is less than 8, output "That is too short"
if it is longer than 16 characters, output "That is too long!"
Create a program that asks the user for a number.
Output whether it is even or odd.
Create a calculator program that asks the user for two numbers.
Then ask the user to enter a maths symbol (+-/*)
Perform the calculation with the two numbers and symbol.
e.g.
User enters 5
User enters 6
User enters *
Output 30
CHALLENGE 1:
Cast the user inputs into floats with float() so decimal numbers will work as well.
CHALLENGE 2:
add an option for modulus (%) and Integer division (//)
CHALLENGE 3:
For the maths symbol option, allow the user to enter the symbol ("*") or the word("Multiply") (HINT: Use the Keyword OR)