Skip to main content

Command Palette

Search for a command to run...

If Statement in Python

Published
•10 min read•View as Markdown
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:

  1. Logical AND: and

  2. Logical OR: or

  3. Logical NOT: not

  1. 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
  1. You used the input() function twice to get user input for two numbers. The values entered by the user are converted to integers using int() and stored in the variables num1 and num2.

  2. The if statement tests whether num1 is greater than num2. If this condition is true, the code block indented under the if statement is executed. It prints the second number (num2) first, followed by the first number (num1).

  3. If the condition in the if statement is false (i.e., num1 is not greater than num2), the code block indented under the else statement is executed. It prints the first number (num1) first, followed by the second number (num2).

  1. 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
  1. You used the input() function to get user input for a number. The value entered by the user is converted to an integer using int() and stored in the variable num1.

  2. The if statement checks if num1 is greater than or equal to 20. If this condition is true (i.e., the number is 20 or more), the code block indented under the if statement is executed. It prints the message "Too high."

  3. If the condition in the if statement is false (i.e., the number is under 20), the code block indented under the else statement is executed. It prints the message "Thank you."

For example, if the user enters num1 = 63, the output will be:

  1. 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
  1. You used the input() function to get user input for a number. The value entered by the user is converted to an integer using int() and stored in the variable num1.

  2. The if statement checks if num1 is 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 the if statement is executed. It prints the message "Thank you."

  3. 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 else statement is executed. It prints the message "Incorrect answer."

For example, if the user enters num1 = 45, the output will be:

  1. 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
  1. You used the input() function to get user input for their favorite color. The value entered by the user is stored in the variable colour.

  2. 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."

  3. The if statement checks if colour is equal to "red" (regardless of case). If this condition is true, the code block indented under the if statement is executed. It prints the message "I like red too."

  4. If the condition in the if statement is false (i.e., the color is not "red"), the code block indented under the else statement 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:

  1. 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
  1. You used the input() function to get user input for whether it's raining. The value entered by the user is converted to lowercase using str.lower() and stored in the variable raining.

  2. The first if statement checks if raining is equal to "yes." If this condition is true, the code block indented under the if statement is executed. It asks the user if it's windy and processes the second part of the logic.

  3. The second if statement checks if windy is 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."

  4. If the user answered "no" to the first question, the code block indented under the else statement is executed. It prints "Enjoy your day."

For example, if the user enters raining = "Yes" and windy = "No", the output will be:

  1. 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
  1. You used the input() function to get user input for their age. The value entered by the user is converted to an integer using int() and stored in the variable age.

  2. The first if statement checks if the age is 18 or older. If this condition is true, it prints the message "You can vote."

  3. If the first condition is not met, the program moves to the first elif statement, which checks if age is exactly 17. If this condition is true, it prints the message "You can learn to drive."

  4. If the first two conditions are not met, the program moves to the second elif statement, which checks if age is exactly 16. If this condition is true, it prints the message "You can buy a lottery ticket."

  5. If none of the above conditions are met, the program moves to the else statement 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:

  1. 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
  1. You used the input() function to get user input for a number. The value entered by the user is converted to an integer using int() and stored in the variable num.

  2. The first if statement checks if num is less than 10. If this condition is true, it prints the message "Too low."

  3. If the first condition is not met, the program moves to the first elif statement, which checks if num is between 10 and 20 (inclusive). If this condition is true, it prints the message "Correct."

  4. If none of the above conditions are met, the program moves to the else statement and prints the message "Too high."

For example, if the user enters num = 16, the output will be:

  1. 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
  1. You used the input() function to get user input for a number. The value entered by the user is converted to an integer using int() and stored in the variable num.

  2. The first if statement checks if num is equal to 1. If this condition is true, it prints the message "Thank you."

  3. If the first condition is not met, the program moves to the first elif statement, which checks if num is equal to 2. If this condition is true, it prints the message "Well done."

  4. If the second condition is not met, the program moves to the second elif statement, which checks if num is equal to 3. If this condition is true, it prints the message "Correct."

  5. If none of the above conditions are met, the program moves to the else statement 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:

H

nice works thanks for sharing useful knowledge

3