Python Basics
Variables & Comments Math Operators Sequence Data Types Strings Lists & Tuples Logical Operators Conditional Statements Loops Functions
Data Wrangling
Pandas Introduction Importing Data Select, Drop & Rename Joining Data Cleaning Data Add Columns Dates
Visualisation
Coming Soon
Machine Learning
Coming Soon
Other Tutorials & Content
Python Data Science Reference Blog



Python Conditional Statements

IF, ELSE and ELIF statements (conditional statements) allow us to build algorithms to create different outputs depending on whether a logical statement, such as whether a variable is equal to a certain value, is true or false. The statements can be based on the values stored in a variable or multiple variables and we can stack these statements to create complex decision logic within our code.

In real life we might decide to wear a coat when we leave home if the temperature is forecast to be below 10 degrees and not wear a coat if the temperature is 10 degrees or above. Here we evaluate the statement that the temperature is below 10 degrees and determine if it is true or false. If it’s true then the outcome is that we will leave home with a coat on. The process works the same when we program with IF, ELSE & ELIF statements. Let’s look at how to implement these statements in Python.

View Our Profile on Datasnips.com to See Our Data Science Code Snippets

Python IF Statement

temp = 5

if temp < 10:
print("Take a coat!")
> Take a coat!

In the above scenario, if the logical statement that temp < 10 is true then we get an output that tells us to wear a coat. If the statement is false however then nothing happens but we might want a different output in this case. We can do this using an ELSE statement.

Python IF ELSE Statement

temp = 18

if temp < 10:
print("Take a coat!")
else:
print("Leave the coat at home!")
> Leave the coat at home!

Python IF ELIF Statement

Now suppose the temperature is forecast to be above 10 degrees, making the statement evaluate to false. However, instead of just leaving our coat at home we want to also check if there is rain forecast. If this is true then we’ll want to take a waterproof. We can implement this using an ELIF statement.

The ELIF statement allows us to stack multiple conditional statements to handle potentially complex decisions in our code.

temp = 18
rain_forecast = True

if temp < 10:
print("Take a coat!")
elif rain_forecast:
print("Take a waterproof!")
else:
print("Leave the coat at home!")
> Take a waterproof!

Python IF Statement – Multiple Conditions

In our examples above, we only considered one variable in our statements but we can use as many as required to define the condition we are trying to evaluate. How these evaluate will depend on which of the operators (OR, AND, NOT) we use and in what combination.

Python IF OR Statement

temp = 18
hours_away_from_home = 10

if temp < 10 or hours_away_from_home > 6:
print("Take a coat!")
else:
print("Leave the coat at home!")
> Take a coat!

Python IF AND Statement

temp = 18
hours_away_from_home = 10

if temp < 10 and hours_away_from_home > 1:
print("Take a coat!")
else:
print("Leave the coat at home!")
> Leave the coat at home!

Python IF NOT Statement

temp = 18
hours_away_from_home = 10

if not(temp <= 10):
print("Take a coat!")
else:
print("Leave the coat at home!")
> Leave the coat at home!
Next