Academic Resource Center

If/Else Statements in Python

Updated on

Knowing how to use if/else statements (also known as conditional statements) is one of the most fundamental skills to have in most forms of programming. They can be a very powerful tool, allowing your program to take an action depending on a specific condition.

Conditional Statements

A conditional statement is a statement that can either be true or false. As an example of this, say you want to compare the two values of x and y and you want to know if x is smaller than y. In Python, the solution to the problem might look something like this:

isXSmaller = (x < y)

The snippet of code below is comparing x to y which results in the variable isXSmaller being equal to either True or False depending on the values of x and y. So, if x = 2 and y = 3, then isXSmaller would be equal to True. Likewise, if x = 6 and y = 2, then isXSmaller would be equal to False.

*Note: True and False values are a separate data type in Python, just like how an integer is a different data type then a string. These are called “Booleans” and should not be mistaken with strings.

The “if” Statement

The if statement takes a True or False statement and either runs or ignores a section of code. Here is an example of an if statement in Python:

Python IfElse Statmetents.pdf - Google Drive - Google Chrome

Figure 1.1 if statement

Python IfElse Statmetents.pdf - Google Drive - Google Chrome

Figure 1.2 if statement result

As can be seen in Figure 1.1 and Figure 1.2, since x is smaller than y, the if statement runs the code and prints out “x is smaller”. Bear in mind that if statements can only take True or False data. In this case, the true or false statement is x < y. Something like if 1 + 2: wouldn’t work because the statement 1 + 2 returns an Integer and not a True or False statement.

The “else” Statement

The else statement is like the if statement in that it can choose to run a section of code or not but differs in that it does not work without an if statement and doesn’t take a True or False value. Here is an example in Python.

Python IfElse Statmetents.pdf - Google Drive - Google Chrome

Figure 2.1 if else statement

Python IfElse Statmetents.pdf - Google Drive - Google Chrome

Figure 2.2 if else statement result

As can be seen in Figure 2.1 and Figure 2.2, since x is bigger than y, the if statement fails, so the print statement that says “x is smaller” doesn’t run. This is then pushed off to the else statement, which sees that the if statement failed, and runs that code instead. In this case, it prints “x is bigger or the same”.

The “elif” statement

The elif statement basically combines the if statement with the else statement. Like the else statement, it can’t run on its own and needs an if statement to go with it, but unlike the else statement, it can take a True/False statement to determine whether to run its code. Here is an example in Python:

Python IfElse Statmetents.pdf - Google Drive - Google Chrome

Figure 3.1 if, elif, else statement

Python IfElse Statmetents.pdf - Google Drive - Google Chrome

Figure 3.2 if, elif, else statement result

As can be seen in Figure 3.1 and Figure 3.2, since x and y are equal to the same number, the if statement first fails because x is not smaller then y. Next the statement tests the elif statement, which also fails because x is not bigger than y. Finally, since both the if and elif statements fail, the final else statement is run, which successfully prints “x is the same”.

Nested “if” statements

One interesting thing you can do with if statements is that you can put an if statement in another if statement. This is called a nested if statement and is very useful for checking multiple conditions. Here is an example in Python:

Python IfElse Statmetents.pdf - Google Drive - Google Chrome

Figure 4.1 Nested if statement code

Python IfElse Statmetents.pdf - Google Drive - Google Chrome

Figure 4.2 Nested if statement result

As can be seen in figure 4.1 and figure 4.2, the first if statement checks to see if x is bigger than y. If x was smaller than y, then the nested if statement would never even be run. In this case though, the test succeeds and the second if statement is run. The second nested if statement then checks if x is bigger than z, that succeeds and so the string “x is bigger than x and y” is printed.

Common Mistakes

Mistake Example Description Fix
Incorrect Indenting if x > 3:
print(“hi”)

OR

if x > 3:
print(“hi”)
Incorrectly indenting an if statement may lead to a syntax error or a bug, which can be much harder to fix. This occurs when you incorrectly indent the if statement itself, or the code you want contained in the if statement. Code you want in your if
statement should be indented relative to the if statement. Here is an example of some properly indented code:
If x > 3:
Not using a
True/False
statement
if 4 + 2:
print(“hi”)

OR

if 5:
print(“hi”)
Failing to use True or False
statements for if statements may cause some tricky to find bugs. This is because doing this might not cause an error, but basically tells the if statement to run every time without any condition in which it will not run.
You should make sure you know what you want your if statement to do. If you want it to run every
time, you might not even want an if statement there. Here is an example.
if 4 + 2 > x:
print(“4 + 2 is bigger than x”)
Not including a colon if x > 3
print(“hi”)
Failing to include a colon after the True/False statement may cause an error. Always put a colon after every if else statement. Here is an example.
if x > 3:
print(“hi”)

Need More Help?

Click here to schedule a 1:1 with a tutor, coach, and or sign up for a workshop. *If this link does not bring you directly to our platform, please use our direct link to "Academic Support" from any Brightspace course at the top of the navigation bar. 

Previous Article While Loop in Python
Next Article Getting Started: R and RStudio, Part One
Still Need Help? Schedule a service in the Academic Support Center