← Python10 min read
If, Elif, and Else
Decision making is an essential part of any program and can be found in every programming language. You could also argue that it can be found in HTML code as well with the way that links and buttons work. This programming concept is often referred to as 'selection' because the statements you're writing are selecting the specific block of code to run. There are a few things you need to know about Python in order to use these decision statements correctly.

Decision making is an essential part of any program and can be found in every programming language. You could also argue that it can be found in HTML code as well with the way that links and buttons work. This programming concept is often referred to as ‘selection’ because the statements you’re writing are selecting the specific block of code to run. There are a few things you need to know about Python in order to use these decision statements correctly.
Using The if Keyword
In Python, like most languages, the if is used. The statement looks at the Boolean statement following the keyword and determines if it is True or False. If it is True, then the rest of the code runs. If not, it will simply skip over the rest of the statement. You can always tell what is part of the if by looking at the tabbing. The ‘inside’ of the if statement is always tabbed over.
num = 2
if num > 0:
print("This code runs since 'num' is greater than 0.")
The statement above can be extended to act like a coin flip decision. In that scenario, it’s going to be one way or another just like a coin flip is either heads or tails. The keyword that you’ll add is the else keyword.
num = 2
if num > 5:
print("This code will not execute.")
else:
print("This code will execute since num is NOT greater than 5.")
The code segment shown above is an example of how an ‘if-else’ decision structure will run. One of the lines, either after the if or after the else will execute. One thing that needs to be mentioned is that the else will always go with the if that is directly above it.
num = 2
if num > 0:
print("This executes first.")
if num > 5:
print("This code will not execute.")
else:
print("This code will execute since num is NOT greater than 5.")
The code segment shown above has multiple if statements with an else statement at the end. The way it works is that the first if statement runs by itself and will print to the screen. Then the second if statement matches with the else statement and so only the else statement will execute. In this example 2 statements will run instead of just 1 statement like the previous example.
Using The elif Keyword
One thing that should be noted in the examples above is that following the else keyword, there isn’t a condition that follows it. The else statement will execute whenever the if statement above it does not execute due to its condition being False. There is a way to create a condition for the else though, and that’s to use an elif statement. The keyword elif is short for ‘else if’.
num = 2
if num > 10:
print("This will not execute.")
elif num > 5:
print("This code will not execute.")
else:
print("This code will execute since num is NOT greater than 10 AND NOT greater than 5.")
In the example shown above, the entire statement acts as one decision making statement. Since the middle statement is an elif statement, it will tie together the if statement at the top and the else statement at the bottom.
Going 1 Step Further with a Dictionary
One thing that can be observed is that decision making statements can get very long because each condition will end up taking up a lot of room. One thing that can be done is to use a ternary operator and cram the if and else statement in one line.
num = 2
print("This code runs since 'num' is greater than 0.") if num > 0 else print("This doesn't print.")
The above example demonstrates how a ternary operator will work in Python. Although space can be condensed, the code looks confusing and a little sloppy; because of this these kind of statements are not encouraged.
A better way to condense decision making statements is to plan it out ahead and use a dictionary. When using a dictionary, you define the input parameters and the output parameters in advance.
myDict = {
0: "The number is 0.",
4: "The number is 4.",
6: "The number is 6."
}
num = 4
print(myDict[num])
The way the code works above is when the print runs, it will use the value stored to num and look for it in the dictionary, myDict. When it finds the 4, it will return the String that is paired with the value of 4. By returning that value, the print statement will output the correct String to the screen.
Keep coding!
Ryan