Tutorial Playlist

Python Tutorial for Beginners


The Best Tips for Learning Python

Lesson - 1

How to Install Python on Windows?

Lesson - 2

Top 10 Python IDEs in 2022: Choosing The Best One

Lesson - 3

A Beginner’s Guide To Python Variables

Lesson - 4

Understanding Python If-Else Statement

Lesson - 5

Python Numbers: Integers, Floats, Complex Numbers

Lesson - 6

Python Strings | Simplilearn Python Tutorial

Lesson - 7

The Basics of Python Loops

Lesson - 8

Python For Loops Explained With Examples

Lesson - 9

Introduction to Python While Loop

Lesson - 10

Everything You Need to Know About Python Arrays

Lesson - 11

All You Need To Know About Python List

Lesson - 12

How to Easily Implement Python Sets and Dictionaries

Lesson - 13

A Handy Guide to Python Tuples

Lesson - 14

Everything You Need to Know About Python Slicing

Lesson - 15

Python Regular Expression (RegEX)

Lesson - 16

Learn A to Z About Python Functions

Lesson - 17

Objects and Classes in Python: Create, Modify and Delete

Lesson - 18

Python OOPs Concept: Here's What You Need to Know

Lesson - 19

An Introduction to Python Threading

Lesson - 20

Getting Started With Jupyter Network

Lesson - 21

PyCharm Tutorial: Getting Started with PyCharm

Lesson - 22

The Best NumPy Tutorial for Beginners

Lesson - 23

The Best Python Pandas Tutorial

Lesson - 24

An Introduction to Matplotlib for Beginners

Lesson - 25

The Best Guide to Time Series Analysis In Python

Lesson - 26

An Introduction to Scikit-Learn: Machine Learning in Python

Lesson - 27

A Beginner's Guide To Web Scraping With Python

Lesson - 28

Python Django Tutorial: The Best Guide on Django Framework

Lesson - 29

Top 10 Reason Why You Should Learn Python

Lesson - 30

10 Cool Python Project Ideas For Beginners in 2021

Lesson - 31

The Best Ideas for Python Automation Projects

Lesson - 32

12 Tips On How To Become a Python Developer

Lesson - 33

The Best Guide for RPA Using Python

Lesson - 34

Comprehending Web Development With PHP vs. Python

Lesson - 35

The Best Tips for Learning Python - REMOVE

Lesson - 36

The Best Way to Learn About Box and Whisker Plot

Lesson - 37

An Interesting Guide to Visualizing Data Using Python Seaborn

Lesson - 38

The Complete Guide to Data Visualization in Python

Lesson - 39

Everything You Need to Know About Game Designing With Pygame in Python

Lesson - 40

The Complete Simplified Guide to Python Bokeh

Lesson - 41

Top 150 Python Interview Questions and Answers for 2022

Lesson - 42

The Supreme Guide to Understand the Workings of CPython

Lesson - 43
Understanding Python If-Else Statement

Decision making is an essential concept in any programming language and is required when you want to execute code when a specific condition is satisfied. In this blog, you will learn about the famous if-else statement in Python. We’ll be using Jupyter Notebook to demonstrate the code.

There are multiple forms of if-else statements. Let’s explore them one by one.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

If Statement

It uses the if keyword, followed by the condition.


if condition:

#statement to execute if the condition is true

Below is the entire workflow of how if statements work:


First, the test expression is checked. If the expression is true, the body of the if the statement is executed. If it is false, the statement present after the if statement is executed. In either case, any line of code present outside if the statement is evaluated by default.

To understand this better, we’ll use an example:


if a>50:

    print("This is the if body")

print("This is outside the if block")

Since 20 is not greater than 50, the statement present inside the if block will not execute. Instead, the statement present outside the if block is executed.


In the code below, both the print statements will be executed since a is greater than 50.


So far, we could specify the statements that will be executed if a condition is true. Now, if you want to evaluate statements that determine whether a condition is actual and if a separate set of statements is false, you can use the if-else conditional statement.

If-else Statement

The if-else statement is used to execute both the true part and the false part of a given condition. If the condition is true, the if block code is executed and if the condition is false, the else block code is executed.



#Executes this block if the condition is true


#Executes this block if the condition is false

You should note here that Python uses indentation in both the blocks to define the scope of the code. Other programming languages often use curly brackets for this purpose.

Below is the entire workflow of how the if-else statement works.


First, the test expression is checked. If it is true, the statements present in the body of the if block will execute. Next, the statements present below the if block is executed. In case the test expression has false results, the statements present in the else body are executed, and then the statements below the if-else are executed.


The following is an example that better illustrates how if-else works:


Since the value of “i” is divisible by two, the if statement is executed.

Since the value of “i” is not divisible by two, the else statement is executed.

Let us now look at what a nested IF statement is and how it works.

Nested IF Statement

When an if a statement is present inside another if statement, it is called a nested IF statement. This situation occurs when you have to filter a variable multiple times.


if (condition1):

    #Executes if condition1 is true

    if (condition2):

        #Executes if condition2 is true

    #Condition2 ends here

#Condition1 ends here

In nested IF statements, you should always take care of the indentation to define the scope of each statement. You can have as many levels of nesting as required, but it makes the program less optimized, and as a result, can be more complex to read and understand. Therefore, you should always try to minimize the use of nested IF statements.

The workflow below demonstrates how nested IF statements work:


The following is another example that shows how nested IF works: We have a number, and we’re going to check if the number is greater or less than 25. If the number is less than 25, we’ll check if it is an odd number or an even number. If the number is greater than 25, we will print that the number is greater than 25.


So far, with IF and if-else, we have only seen a binary approach. Suppose we have a problem that has multiple conditions. In this scenario, the if-elif-else statement comes to the rescue.

FREE Data Science With Python Course

Start Learning Data Science with Python for FREEStart Learning
FREE Data Science With Python Course

If-Elif-Else Statement

It checks the if statement condition. If that is false, the elif statement is evaluated. In case the elif condition is false, the else statement is evaluated.


if (condition):


elif (condition):






Below is a flowchart that shows how the if-elif-else ladder works. The Test Expression1 is checked. If that proves true, the body of if is evaluated. If it is false, then the control moves to the proceeding Test Expression2. If it’s true, the body of elif1 is executed. If it’s false, the test expression3 is checked. If true, the body of elif2 is executed. If it is false, the body of else is evaluated. Any statement below in if-elif is then checked.


The program below uses the if-elif-else ladder to check if a letter is a vowel or a consonant.


Now that we have looked at the basics of if, else, elif and nested IF, let’s do some exercises.

Free Course: Programming with Python

Learn the Basics of Programming with PythonEnroll Now
Free Course: Programming with Python

Program to Check the Greatest among Three Numbers


In the above code, we first check if 'a' is higher than both 'b' and 'c'. Next, we print 'a' as the greatest number.

In case it is false, we then check if 'b' is greater than both 'a' and 'c'. If this is true, we print 'b' as the greatest number. Otherwise, 'c' is the greatest number.

The same program can be created using the nested IF statement as follows:


Here is one more exercise that can allow you to check whether a number is divisible by two, three, or five.


The problem with the code above is that 12 is also divisible by three, but we are unable to print it. In that case, we only need to use the if statement.


The last output statement is incorrect since the output is divisible by two and three. To fix this issue, use a counter variable.



I hope this blog helped you understand conditional statements in Python. You learned about if, else, if-elif-else and nested IF statements and practiced with a few hands-on exercises. To learn more, watch this Python If Else Statement.  To get more in-depth training in Python programming, take our Python Training Course.

About the Author

Avijeet BiswalAvijeet Biswal

Avijeet is a Senior Research Analyst at Simplilearn. Passionate about Data Analytics, Machine Learning, and Deep Learning, Avijeet is also interested in politics, cricket, and football.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.