Python if else Tutorial: Control the Flow of Your Code
When programming, controlling the flow of what code is run under what circumstance is extremely important. The Python if
else
commands act like a digital traffic cop, letting you define blocks of code that run when certain conditions are met. The if
else
syntax is one of the most important pieces of Python syntax that you'll learn.
In this tutorial, you'll learn how to use Python if
else
to control your code. We're assuming you already know a few Python basics such as:
- How to read a CSV file
- Basic Python types like lists, strings and integers
- Using for loops to work with lists.
If you aren't comfortable with those yet, we recommend this free and interactive Python Fundamentals course, which teaches all of these (and Python if
else
as well!)
The Taco Dataset
We'll be learning how to use Python if
else
while working with a dataset that summarizes virtual tacos used within Dataquest's online chat for a specific month.
At Dataquest, we give virtual tacos in Slack (using HeyTaco) as a way of saying thank you or rewarding colleagues on a job well done. You can give a taco to someone to show your gratitude with a message, like this:
We'll be doing some analysis of data from HeyTaco to answer some basic questions about people's giving habits. The data set is stored in the CSV file "tacos.csv"
, and you can download it here if you'd like to follow along with this tutorial. (In this data set, we have changed the names to protect the privacy of Dataquest staffers).
Let's start by reading in the CSV file and looking at the first few lines of the file:
import csv
f = open('tacos.csv')
tacos = list(csv.reader(f))
print(tacos[:5])
Each line (or row) represents a single person who works for the company. The data set has four columns:
-
name
: The name of the person (these names are fictional, but the data represents actual employees at Dataquest!) -
department
: What department (or team) the person works for. -
given
: The number of tacos that person gave to others. -
received
: The number of tacos that person received from others
Let's remove the first row, since it includes column names — the structure of our data is simple so we'll be able to remember them as we go (or refer to the top of the post if you forget!)
After we remove the column names, let's look at the first five rows of our data again:
tacos = tacos[1:]
print(tacos[:5])
Preparing the data
Even though the data in the third and fourth columns (representing the number of tacos each person has given and received) are numbers, they are stored as strings. We can tell they're strings because of the quotes around them: '4'
instead of 4
.
In order to perform calculations on the data, we'll need to make them into integers, a numeric Python type.
Let's use a for
loop to iterate over every row of our data and convert columns 3 and 4 (located at indexes 2 and 3) to the integer type:
for person in tacos:
person[2] = int(person[2])
person[3] = int(person[3])
print(tacos[:5])
You can now see the quotes are removed (eg 4
), indicating these values are now integers instead of strings.
Finding Averages in our Data
Let's start with some basic analysis — finding the average number of tacos each person has given and received.
To do this, we'll extract the given and received columns into individual lists so we can calculate more easily:
given = []
received = []
for person in tacos:
given.append(person[2])
received.append(person[3])
print(given[:5])
[4, 7, 31, 13, 38]
Next, we'll take these two lists, sum them, and then divide by the length (or number of values) to find averages:
given_avg = sum(given) / len(given)
received_avg = sum(received) / len(received)
print("Avg tacos given: ", given_avg)
print("Avg tacos received: ", received_avg)
Avg tacos given: 16.322580645161292 Avg tacos received: 16.322580645161292
The average number of tacos given and received are identical! This makes sense when you think about it, since every taco given by someone has to be received by someone else.
Another question we might be interested in answering is how the averages of the different departments in the company compare in their giving and receiving. Let's start by examining the 'content' team.
To do this, we need to extract lists of given and received tacos like we did before, but only when the department of the row is 'content'. What we just described is called a condition, and we're going to need to use a Python if
to check that condition!
Python if
You can think of the Python if
as being a decision. In our example, we need to ask a question: Does this person belong to the 'content' team? The action we take in our code depends on the answer to this question, or the condition. This is why Python if
's are sometimes also called conditional expressions.
The diagram below shows the logic that we'll need to use to create a list of the values that match our condition:
Let's look at how we can use the Python if
with two separate rows. First let's print the first and second row so we can remind ourselves of their values:
first_row = tacos[0]
print(first_row)
['Amanda', 'content', 4, 3]
second_row = tacos[1]
print(second_row)
['Angela', 'engineering', 7, 20]
The first row contains Amanda from the content team, while the second row contains Angela from the engineering team. Let's look at how we use Python if
syntax to print some output only if the person comes from the content team.
We'll use the ==
operator to compare the team to the string 'content'. The ==
operator in Python means "is equal to".
Some other common operators we can use with if
conditions include:
-
!=
: Is not equal to -
>
: Is greater than -
<
: Is less than -
>=
: Is greater than or equal to -
<=
: Is less than or equal to
team = first_row[1]
if team == 'content':
print("This person comes from the content team.")
This person comes from the content team.
Because Amanda comes from the content team, our print()
function executed and we saw output. Let's trace the path from our earlier diagram to understand what happened:
Let's take a moment to look a little bit closer at the syntax we used and label the different parts so we can understand what happened.
Now we understand the code a bit better, let's try the same code with the second row and see what happens:
team = second_row[1]
if team == 'content':
print("This person comes from the content team.")
When we ran the code above, we didn't get any output because Angela is from the engineering team, not the content team. Let's trace the path from our earlier diagram to understand what happened.
Using Python if With a For Loop
Now that we understand the basics of how Python if
works, let's use it inside a loop to get the 'given' and 'received' values from just the content team:
given_content = []
received_content = []
for person in tacos:
team = person[1]
if team == 'content':
given_content.append(person[2])
received_content.append(person[3])
print(given_content)
[4, 25, 10, 6, 0, 16, 8, 32]
We printed the given_content
list above and we can see that the values for the 8 members of the content team have been collected together. Let's now calculate the team averages:
given_content_avg = sum(given_content) / len(given_content)
received_content_avg = sum(received_content) / len(received_content)
print("Avg tacos given, content team: ", given_content_avg)
print("Avg tacos received, content team: ", received_content_avg)
Avg tacos given, content team: 12.625 Avg tacos received, content team: 6.0
We can see that content team members give tacos about twice as often as they receive them. We can also compare these numbers to the overall averages and find:
- Content team members give tacos about 25% less than the overall average
- Content team members receive tacos about 60% less than the overall average
Using Python if else to Improve Our Analysis
When we compared content team members to the overall average, that overall average included the content team members. It might be interesting to compare the content team with everyone not in the content team.
To do this, we'll need to use a new part of a Python if
- the else
clause. The else clause comes after the if
, and specifies one or more lines of code that you want to run if the condition in the if
doesn't match.
Let's look at our diagram from earlier to see what an added else
clause looks like:
Let's modify our code from earlier that looked at just the second row to add an else
clause. Before we start, let's quickly remind ourselves of the contents of the second row
print(second_row)
['Angela', 'engineering', 7, 20]
OK, let's add the else
clause:
team = second_row[1]
if team == 'content':
print("This person comes from the content team.")
else:
print("This person doesn't come from the content team.")
This person doesn't come from the content team.
You can see that the code in our else
clause was executed because Angela doesn't belong to the content team.
Let's trace the path in our diagram from earlier:
Lastly, let's add an else
clause to our loop and calculate the averages of both groups:
given_content = []
received_content = []
given_other = []
received_other = []
for person in tacos:
team = person[1]
if team == 'content':
given_content.append(person[2])
received_content.append(person[3])
else:
given_other.append(person[2])
received_other.append(person[3])
given_content_avg = sum(given_content) / len(given_content)
received_content_avg = sum(received_content) / len(received_content)
given_other_avg = sum(given_other) / len(given_other)
received_other_avg = sum(received_other) / len(received_other)
print("Avg tacos given, content team: ", given_content_avg)
print("Avg tacos given, other teams: ", given_other_avg)
print("Avg tacos received, content team: ", received_content_avg)
print("Avg tacos received, other teams: ", received_other_avg)
Avg tacos given, content team: 12.625 Avg tacos given, other teams: 17.608695652173914 Avg tacos received, content team: 6.0 Avg tacos received, other teams: 19.91304347826087
We can see that the content team gives about 30% less tacos than other teams, and receives about 70% less tacos than other teams receive.
Python elif
What if we wanted to calculate the tacos given and received for:
- The content team
- The engineering team
- All other teams
To do this, we need a new tool: the Python elif
. The elif
clause, like the else
clause, must come after an if
. It allows us to stack a second condition that is assessed only if the first condition is not met. This might sound confusing at first, but when you think about it's name — else if — you can understand that it's a shortcut for adding another if
inside an else
.
Let's look at our diagram from earlier to see what an added elif
clause looks like:
Let's add an elif
to our stand-alone code that checks whether someone is in the content team or the engineering team. First, let's quickly remind ourselves of the content of the second row again:
print(second_row)
['Angela', 'engineering', 7, 20]
Let's add the elif
clause:
team = second_row[1]
if team == 'content':
print("This person comes from the content team.")
elif team == 'engineering':
print("This person comes from the engineering team.")
else:
print("This person doesn't come from the content or engineering teams.")
This person comes from the engineering team.
You can see that the code in our elif
clause was executed because Angela belongs to the engineering team.
Let's trace the path in our diagram from earlier:
Lastly, let's add an elif
clause to our loop and calculate the averages of all three groups:
given_content = []
received_content = []
given_engineering = []
received_engineering = []
given_other = []
received_other = []
for person in tacos:
team = person[1]
if team == 'content':
given_content.append(person[2])
received_content.append(person[3])
elif team == 'engineering':
given_engineering.append(person[2])
received_engineering.append(person[3])
else:
given_other.append(person[2])
received_other.append(person[3])
given_content_avg = sum(given_content) / len(given_content)
received_content_avg = sum(received_content) / len(received_content)
given_engineering_avg = sum(given_engineering) / len(given_engineering)
received_engineering_avg = sum(received_engineering) / len(received_engineering)
given_other_avg = sum(given_other) / len(given_other)
received_other_avg = sum(received_other) / len(received_other)
print("Avg tacos given, content team: ", given_content_avg)
print("Avg tacos given, engineering team: ", given_engineering_avg)
print("Avg tacos given, other teams: ", given_other_avg)
print() # this prints an empty line
print("Avg tacos received, content team: ", received_content_avg)
print("Avg tacos received, engineering team: ", received_engineering_avg)
print("Avg tacos received, other teams: ", received_other_avg)
Avg tacos given, content team: 12.625 Avg tacos given, engineering team: 20.166666666666668 Avg tacos given, other teams: 16.705882352941178 Avg tacos received, content team: 6.0 Avg tacos received, engineering team: 26.166666666666668 Avg tacos received, other teams: 17.705882352941178
Our analysis shows that while the content team given and receive tacos at below the average of the other teams, the engineering team give and receive tacos at a greater than average rate.
Python if else: Next Steps
In this tutorial, we learned:
-
That the Python
if
else
lets us control the flow of our code based on conditions. -
How to use
if
to execute code only if it matches a condition. -
How to use
else
to execute code only if it doesn't match a condition.
You might like to extend this tutorial and practice using Python if
else
by calculating the averages for each of the teams in the dataset.
If you'd like to learn about Python if
else
in an interactive tutorial, you can learn how while analyzing app data in our free, interactive Python Fundamentals course.