If Statement in Python

Explanation
If statements allow your program to make a decision and change the route that is taken through the program.
Indenting Lines of Code
In order to indent text you can use your [Tab] key or you can press your [space key] five times. The [backspace] key will remove indents. The first line of the if statement tests a condition, and if that condition is met (i.e. the first condition is true) then the lines of code directly below it are run. If it is not met (i.e. the first condition is false) it will test the second condition, if there is one, and so on. Below are examples of the different comparison and logical operators you can use in the condition line of your if statement.
Comparison Operators
Equal to:
==Not equal to:
!=Greater than:
>Less than:
<Greater than or equal to:
>=Less than or equal to:
<=
Logical Operators:
Logical AND:
andLogical OR:
orLogical NOT:
not
- Ask for two numbers. If the first one is larger than the second, display the second number first and then the first number, otherwise show the first number first and then the second.
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
if num1 > num2:
print("Second number: ",num2,"First number:", num1)
else:
print("First number:", num1, "Second number: ", num2)
# Output:
# Enter the first number: 1
# Enter the second number: 2
# First number: 1 Second number: 2
You used the
input()function twice to get user input for two numbers. The values entered by the user are converted to integers usingint()and stored in the variablesnum1andnum2.The if statement tests whether
num1is greater thannum2. If this condition is true, the code block indented under theifstatement is executed. It prints the second number (num2) first, followed by the first number (num1).If the condition in the if statement is false (i.e.,
num1is not greater thannum2), the code block indented under theelsestatement is executed. It prints the first number (num1) first, followed by the second number (num2).
- Ask the user to enter a number that is under 20. If they enter a number that is 20 or more, display the message “Too high”, otherwise display “Thank you”.
num1 = int(input("Enter a number that is under 20: "))
if num1 >= 20:
print("Too high")
else:
print("Thank you")
# Optput:
# Enter a number that is under 20: 63
# Too high
You used the
input()function to get user input for a number. The value entered by the user is converted to an integer usingint()and stored in the variablenum1.The if statement checks if
num1is greater than or equal to 20. If this condition is true (i.e., the number is 20 or more), the code block indented under theifstatement is executed. It prints the message "Too high."If the condition in the if statement is false (i.e., the number is under 20), the code block indented under the
elsestatement is executed. It prints the message "Thank you."For example, if the user enters
num1 = 63, the output will be:
- Ask the user to enter a number between 10 and 20 (inclusive). If they enter a number within this range, display the message “Thank you”, otherwise display the message “Incorrect answer”.
num1 = int(input("Enter a number between 10 and 20: "))
if num1 > 10 and num1 < 20:
print("Thank you")
else:
print("incorrect answer")
# Optput:
# Enter a number between 10 and 20: 45
# incorrect answer
You used the
input()function to get user input for a number. The value entered by the user is converted to an integer usingint()and stored in the variablenum1.The if statement checks if
num1is greater than or equal to 10 AND less than or equal to 20. If this condition is true (i.e., the number is within the range of 10 to 20, inclusive), the code block indented under theifstatement is executed. It prints the message "Thank you."If the condition in the if statement is false (i.e., the number is outside the range of 10 to 20), the code block indented under the
elsestatement is executed. It prints the message "Incorrect answer."For example, if the user enters
num1 = 45, the output will be:
- Ask the user to enter their favourite colour. If they enter “red”, “RED” or “Red” display the message “I like red too”, otherwise display the message “I don’t like [colour], I prefer red”.
colour = input("Enter their favourite colour : ")
if colour == "red" or colour == "RED" or colour == "Red":
print("I like red too")
else:
print("I don’t like",colour)
# Optput:
# Enter their favourite colour : red
# I like red too
You used the
input()function to get user input for their favorite color. The value entered by the user is stored in the variablecolour.The
lower()method is used to convert the input to lowercase. This ensures that the program can recognize variations such as "RED," "Red," or "red" as equivalent to "red."The if statement checks if
colouris equal to "red" (regardless of case). If this condition is true, the code block indented under theifstatement is executed. It prints the message "I like red too."If the condition in the if statement is false (i.e., the color is not "red"), the code block indented under the
elsestatement is executed. It prints the message "I don’t like [color], I prefer red" with the original input color.For example, if the user enters
colour = "RED", the output will be:
- Ask the user if it is raining and convert their answer to lower case so it doesn’t matter what case they type it in. If they answer “yes”, ask if it is windy. If they answer “yes” to this second question, display the answer “It is too windy for an umbrella”, otherwise display the message “Take an umbrella”. If they did not answer yes to the first question, display the answer “Enjoy your day”.
raining = input("It is raining: ")
raining = str.lower(raining)
if raining == "Yes":
windy = input("it is windly?")
windy = str.lower(windy)
if windy == "yes":
print("iit is too windly for an umberella")
else:
print("Take a umberella")
else:
print("Enjoy your day")
# Optput:
# It is raining: FGHJ
# Enjoy your day
You used the
input()function to get user input for whether it's raining. The value entered by the user is converted to lowercase usingstr.lower()and stored in the variableraining.The first if statement checks if
rainingis equal to "yes." If this condition is true, the code block indented under theifstatement is executed. It asks the user if it's windy and processes the second part of the logic.The second if statement checks if
windyis equal to "yes." If this condition is true, it prints the message "It is too windy for an umbrella." If the user didn't answer "yes" to the second question, it prints "Take an umbrella."If the user answered "no" to the first question, the code block indented under the
elsestatement is executed. It prints "Enjoy your day."
For example, if the user enters raining = "Yes" and windy = "No", the output will be:
- Ask the user’s age. If they are 18 or over, display the message “You can vote”, if they are aged 17, display the message “You can learn to drive”, if they are 16, display the message “You can buy a lottery ticket”, if they are under 16, display the message “You can go Trickor-Treating”
age = int(input("Enter the your age: "))
if age >= 18:
print("You can vote")
elif age == 17:
print("You can learn to drive")
elif age == 16 :
print("You can buy a lottery ticket")
else:
print("You can go Trickor-Treating")
# Optput:
# Enter the your age: 14
# You can go Trickor-Treating
You used the
input()function to get user input for their age. The value entered by the user is converted to an integer usingint()and stored in the variableage.The first
ifstatement checks if theageis 18 or older. If this condition is true, it prints the message "You can vote."If the first condition is not met, the program moves to the first
elifstatement, which checks ifageis exactly 17. If this condition is true, it prints the message "You can learn to drive."If the first two conditions are not met, the program moves to the second
elifstatement, which checks ifageis exactly 16. If this condition is true, it prints the message "You can buy a lottery ticket."If none of the above conditions are met, the program moves to the
elsestatement and prints the message "You can go Trick-or-Treating." This covers any age under 16.For example, if the user enters
age = 14, the output will be:
- Ask the user to enter a number. If it is under 10, display the message “Too low”, if their number is between 10 and 20, display “Correct”, otherwise display “Too high”.
num = int(input("Enter a number: "))
if num < 10:
print("Too low")
elif num >= 10 and num <= 20:
print("Correct")
else:
print("Too high")
# Optput:
# Enter a number: 16
# Correct
You used the
input()function to get user input for a number. The value entered by the user is converted to an integer usingint()and stored in the variablenum.The first
ifstatement checks ifnumis less than 10. If this condition is true, it prints the message "Too low."If the first condition is not met, the program moves to the first
elifstatement, which checks ifnumis between 10 and 20 (inclusive). If this condition is true, it prints the message "Correct."If none of the above conditions are met, the program moves to the
elsestatement and prints the message "Too high."For example, if the user enters
num = 16, the output will be:
- Ask the user to enter 1, 2 or 3. If they enter a 1, display the message “Thank you”, if they enter a 2, display “Well done”, if they enter a 3, display “Correct”. If they enter anything else, display “Error message”.
num = int(input("Enter a number: "))
if num == 1:
print("Thank you")
elif num == 2:
print("Well done")
elif num == 3:
print("Correct")
else:
print("Error")
# Optput:
# Enter a number: 1
# Thank you
You used the
input()function to get user input for a number. The value entered by the user is converted to an integer usingint()and stored in the variablenum.The first
ifstatement checks ifnumis equal to 1. If this condition is true, it prints the message "Thank you."If the first condition is not met, the program moves to the first
elifstatement, which checks ifnumis equal to 2. If this condition is true, it prints the message "Well done."If the second condition is not met, the program moves to the second
elifstatement, which checks ifnumis equal to 3. If this condition is true, it prints the message "Correct."If none of the above conditions are met, the program moves to the
elsestatement and prints the message "Error." This covers any other number entered by the user.For example, if the user enters
num = 1, the output will be:



