Learn Python Episode #21: If, Elif, Else Statements

In this tutorial we are going to be learning about the conditional statements available to you in Python. So, we're going to be learning about the if-else statement, and this is basically provides us a way to evaluate if something is true or false, and then perform something whether or not the condition is met. Let's write an if-else statement.

check = True

if check == False:
    print("It is false")
else:
    print("It is actually equal to True")

This is going to print out "It is actually equal to True" in the console. First, Python is going to check if the variable is equal to false. Else, it's going to continue through the script and print, or perform, whatever we tell it to do. This is great if we only have two conditions, but what if we want to use more than two? Between the if and else we can use an elif statement, and here we can supply an additional condition.

check = Hamburger

if check == False:
    print("It is false")
elif check == "Hamburger":
    print("Yummm, hamburgers")
else:
    print("It is actually equal to True")

When we run this script it's going check to see if the first condition is true, if not it will check the next condition, if not it will print the else statement. So, that's what an if-statement is, and these are necessary for the project we are about to begin working on.

Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/

Best,
Joseph Delgadillo
Web: https://josephdelgadillo.com/
YouTube: https://www.youtube.com/c/JosephDelgadillo
Steemit: https://steemit.com/@jo3potato

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now