Skip to main content
HomeAbout PythonLearn Python

if…elif…else in Python Tutorial

Learn how you can create if…elif…else statements in Python.
Updated Dec 2022  · 4 min read

if…elif…else are conditional statements that provide you with the decision making that is required when you want to execute code based on a particular condition.

The if…elif…else statement used in Python helps automate that decision making process.

if condition

The if condition is considered the simplest of the three and makes a decision based on whether the condition is true or not. If the condition is true, it prints out the indented expression. If the condition is false, it skips printing the indented expression.

if condition

Example of if

Suppose you have a variable z, equal to 4. If the value is 'even', you will print z is 'even'. You will use modulo operator 2, which will return 0 if z is 'even'. As soon as you run the below code, Python will check if the condition holds. If True, the corresponding code will be executed.

example of if

Example of multiple lines inside if statement

It is perfectly fine to have more lines inside the if statement, as shown in the below example. The script will return two lines when you run it. If the condition is not passed, the expression is not executed.

example of multiple lines inside if

Example of a False if statement

Let's change the value of z to be odd. You will notice that the code will not print anything since the condition will not be passed, i.e., False.

example of a false if condition

if-else condition

The if-else condition adds an additional step in the decision-making process compared to the simple if statement. The beginning of an if-else statement operates similar to a simple if statement; however, if the condition is false, instead of printing nothing, the indented expression under else will be printed.

if-else condition

Example of if-else

Continuing our previous example, what if you want to print 'z is odd' when the if condition is false? In this case, you can simply add another condition, which is the else condition. If you run it with z equal to 5, the condition is not true, so the expression for the else statement gets printed out.

example of if-else condition

if-elif-else condition

The most complex of these conditions is the if-elif-else condition. When you run into a situation where you have several conditions, you can place as many elif conditions as necessary between the if condition and the else condition.

if-elif-else condition

Example one of if-elif-else condition

Below is an example of where you want different printouts for numbers that are divisible by 2 and 3.

Here, since z equals 3, the first condition is False, so it goes over to the next condition. The next condition does hold True. Hence, the corresponding print statement is executed.

example of if-elif-else condition

Example two of if-elif-else condition

In the below example, you define two variables room and area. You then construct if-elif-else and if-else conditions each for room and area, respectively.

In the first condition, you check if you are looking in the kitchen, elif you are looking in the bedroom, else you are looking around elsewhere. Depending on the value of the room variable, the satisfied condition is executed.

Similarly, for the area variable, you write an if and else condition and check whether the area is greater than 15 or not.

example of if-elif-else condition 2

When we run the above code, it produces the following result:

looking around in the bedroom. pretty small.

Try it for yourself.

To learn more about elif statements in Python, please see this video from our course, Intermediate Python.

This content is taken from DataCamp’s Intermediate Python course by Hugo Bowne-Anderson.

Python courses

Intermediate Python

Beginner
4 hr
952.8K
Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

Pandas 2.0: What’s New and Top Tips

Dive into pandas 2.0, the latest update of the essential data analysis library, with new features like PyArrow integration, nullable data types, and non-nanosecond datetime resolution for better performance and efficiency.
Moez Ali's photo

Moez Ali

9 min

PyTorch 2.0 is Here: Everything We Know

Explore the latest release of PyTorch, which is faster, more Pythonic, and more dynamic.
Abid Ali Awan's photo

Abid Ali Awan

6 min

An Introduction to Python T-Tests

Learn how to perform t-tests in Python with this tutorial. Understand the different types of t-tests - one-sample test, two-sample test, paired t-test, and Welch’s test, and when to use them.
Vidhi Chugh's photo

Vidhi Chugh

13 min

Matplotlib time series line plot

This tutorial explores how to create and customize time series line plots in matplotlib.
Elena Kosourova's photo

Elena Kosourova

8 min

Step-by-Step Guide to Making Map in Python using Plotly Library

Make your data stand out with stunning maps created with Plotly in Python
Moez Ali's photo

Moez Ali

7 min

High Performance Data Manipulation in Python: pandas 2.0 vs. polars

Discover the main differences between Python’s pandas and polars libraries for data science
Javier Canales Luna's photo

Javier Canales Luna

16 min

See MoreSee More