Python if and if-else Statements

2-6-2021

If statements can be used in Python to control the flow of a program. They are essentially used to execute code only when something is true. Combined with else statements, they can be used to create multiple paths the program can take depending on whether certain statements are true or not. Here is an example of this:


if 3>2:
    print("Hello world")

This is an if statement that is checking if 3 is greater than 2, and if this is true, it prints "Hello World". There are a few things to note about this. First, the if statement starts with the word "if". It is then followed by the condition. This is what is being checked to be true or not. In this case, it is the statement 3>2, which is of course true. This is then followed by a colon to end the first line. The code that is to be executed if the condition is true is then indented on the next line.


"Hello world"

This program prints "Hello, world" because it's condition is true. Look at this one though:


if 2>3:
    print("Hello world")

This will not print out "Hello world" because 2>3 is not true.


With Variables


If statements can also be used with variables. For example:


num1=2
num2=5

if num2>num1:
    print("num2 is greater than num1")

Or:


apples=input("Do you like apples?\n")

if apples=="Yes":
    print("Me too!")

Notice how to check if something is equal to something else we use two equals signs.


The key takeaway of if statements is that code inside of them will only run if the above condition, whatever it may be, evaluates to true.


Else


If statements can also be used in conjunction with the else statement. This gives a program two options. With just an if statement, the program would only execute some code if the condition was true. With an else statement, you can give the program an alternative choice if the if statement is not true. For example:


apples=input("Do you like apples?\n")


if(apples=="Yes:):
    print("Me too!")
else:
    print("Aw darn")

This bit of code here now allows two different messages to be printed out depending on what the user types into the input function. If they type "Yes", the program will print out "Me too!". If they type in anything else, the program will print "Aw darn".


The important thing thing understand with the else statement is that it will execute if the if statement directly above it is not true. Using this, your program will do one thing if a certain condition is met, and it will do something else in any other case.


elif


With if and else, we can have our program take two different paths. But what if we want our program to have three or four different options? Well we can use the elif statement. This is short for "else if", and is place between an if and else statement to add other cases. For example:


fruit=input("What is your favorite fruit?\n")

if fruit=="apple":
    print("I like apples too!")
elif fruit =="banana":
    print("I like bananas too!")
else:
    print("I don't really like that fruit")

This program can now respond appropriately for if the user entered "apple" or "banana", and then has an else statement for any other case. Elifs can also be stacked for as many options as your program needs.


fruit=input("What is your favorite fruit?\n")

if fruit=="apple":
    print("I like apples too!")
elif fruit =="banana":
    print("I like bananas too!")
elif fruit =="grapes":
    print("I like grapes too!")
else:
    print("I don't really like that fruit")

Now there are three cases with specific responses, and the else statement can handle any other case entered! You can add as many elifs as you need for a specific conditional statement.


Conclusion


In conclusion, if statements allow your program to finally do some varying operations. I encourage you too try and implements a small program using if, elif, and else. Try using it along with the input function to have some differing output depending on the user's input. Also, look for a practice project on Anonyocder using these ideas!