Python IF-ELSE Statements

Like other popular programming languages, there are some control flow statements in Python as well. Control flow refers to the order in which the program should be executed. Generally, the control flow of a program runs from top to bottom.

However, the control flow statements break the general top-to-bottom order of execution by including decision-making, looping, and more. This enables the program to first execute a specific block of code based on the conditions used.

Watch this video on Control flow in Python.

Interested in learning Python? Check out the Python Training in Sydney!

In this module, we will learn about if-else control flow statements, and the following is the list of all topics that we will cover.

So, without any further delay, let’s get started.

Python Conditional Statements

Decision-making in programming, much similar to decision-making in real life, is quite important as it helps us decide what the program should do next. Decision-making helps in deciding the flow of execution of the program. Decision-making is implemented using if else in Python. The conditional logic in Python is primarily based on the ‘if else’ structure.

Starting from the if statement, it is the most basic decision-making statement. It simply decides whether a particular code block will be executed or not on the basis of the condition provided in the if statement. If the condition provided in the if statement is true, then the code block is executed, and if it’s false then the code block is not executed.

The following flowchart explains the working of if statement in Python:

Python flowchart

Syntax of the if statement in Python:

if test expression:
statement(s)

As depicted by the flowchart above, the Python program first evaluates the test expression. It is basically the condition in the if statement in Python. If the condition is met or if the condition is true, then only the statement(s) in the body of the if statement is(are) executed.

Note – The body of the if statement in Python starts after an indentation, unlike other languages that use brackets to write the body of if statements.

Become a master of Python by going through this online Python Course in Toronto!

Let’s see an example of the implementation of an if statement.

a = 5
if (a <10):
print (“5 is less than 10”)
print (“Statement after if statement”)


Output:
5 is less than 10
Statement after if statement

Get 100% Hike!

Master Most in Demand Skills Now !

If else in Python

If statement in Python tells the program what to do if the condition is true. In case the condition is false, the program just goes on to execute what comes after if statements. In situations where we want the program to execute some statement if the condition is true and some other states only if the condition is false, then we use if else in Python.

The following flowchart explains the working of if-else in Python:

Syntax of the if-else in Python:

if test expression:

Body of if


else:

Body of else

As depicted by the flowchart above, the Python program first evaluates the test expression. It is basically the condition in the if statement. If the condition is met or if the condition is true, then only the statement(s) in the body of the if statement is(are) executed. If the condition is not true, then the statement in the body of the else statement is executed. The body of if and else statements starts with indentation.

Let’s see an example of the implementation of the if…else statement.

i = 20;

if (i < 15):
print (“i is smaller than 15”)

else:
print (“i is greater than 15”)
print (“statement after if statement”)


Output:
i is greater than 15
statement after if statement

Get certified by this top Python Course in Singapore today!

Career Transition

Non-Tech to IT Associate | Career Transformation | AWS Certification Course - Intellipaat Reviews
Non Tech to DevOps Engineer Career Transition | Intellipaat Devops Training Reviews - Nitin
Upskilled & Got Job as Analyst After a Career Break |  Data Science Course Story - Shehzin Mulla
Successful Career Change after Completion of AWS Course - Krishnamohan | Intellipaat Review
Got Job Promotion After Completing Artificial Intelligence Course - Intellipaat Review | Gaurav
Intellipaat Reviews | Big Data Analytics Course | Career Transformation to Big Data | Gayathri

If Elif Else in Python

Here, the elif stands for else if in Python. This conditional statement in Python allows us to check multiple statements rather than just one or two like we saw in if and if-else statements. If first if the condition is true, then same as in the previous if and if-else statements, the program will execute the body of the if statement.

Otherwise, the program will go to the elif block (else if in Python) which basically checks for another if statement. Again, if the condition is true, the program will execute the body of the elif statement, and if the condition is found to be false, the program will go to the next else block and execute the body of the else block.

If elif else ladder:

When there is more than just two if conditions in if elif else statements, then it is referred to as if elif else ladder, as it resembles the structure of a ladder in terms of if statements. If one of the if conditions turn out to be true, then the rest of the ladder is just bypassed and the body of that particular if block is executed. If all the if conditions turn out to be false, then the body of the last else block is executed.

The following flowchart depicts the working of if elif else statements:


Syntax of the if elif else in Python

if test expression:

Body of if

elif test expression:

Body of elif

else:

Body of else

We can put as many elif statements as our program requires before the last else statements, making it an if elif else ladder.

Let’s see the following example of the if elif else statement to get a better understanding.

a = 50

if (a == 20):
print (“value of variable a is 20”)

elif (a == 30):
print (“value of variable a is 30”)

elif (a == 40):
print (“value of variable a is 40”)

else:
print (“value of variable a is greater than 40”)


Output:
value of variable a is greater than 40

In the above example, the program first checks the very first if statement. Since it turns out to be false, the body of the if statement is not executed, and the program goes to the next elif statement. It also turns out to be false and again the body of the elif block is skipped, and the program goes to the next elif statement. The same thing happens here. Since all conditions were false, the program finally reaches the last else statement and executes the body of else. So, we get the output as ‘value of variable a is greater than 40’.

Learn end-to-end Python concepts through the Python Course in Hyderabad to take your career to a whole new level!

Nested If-else in Python

As the name suggests,  nested if-else statements are nested inside other if statements. That is, a nested if statement is the body of another if statement. We use nested if statements when we need to check secondary conditions only if the first condition executes as true.
The following flow chart depicts the working of nested if statements.

Syntax of nested if in Python:

if test expression 1:
# Executes when condition 1 is true
body of if statement


if test expression 2:
# Executes when condition 2 is true
Body of nested if


else:
body of nested if


else:
body of if else statement

Let’s see the following example of if in Python:

a = 20
if (a == 20):


# First if statement
if (a < 25):
print (“a is smaller than 25”)


else:
print (“a is greater than 25”)


else:
print (“a is not equal to 20”)


Output:
a is smaller than 25

Here in this example, let us discuss the above example of nested if in Python. Since a is 20, the program enters in the first if statement. Then it checks the nested if statements and executes it as true and prints that a is smaller than 25 on the screen.

Now, the nested else and else statements won’t be executed, and the program will go to the statement after the end of the if block. That is how nested if condition in Python works.

Are you interested in learning Python from experts? Enroll in our Python Course in Bangalore now!

Shorthand If and If Else in Python

Shorthand if and if else is nothing but a way to write the if statements in one line when we have only one statement to execute in the if block and the else block.

An example for shorthand if in Python:

a = 4
b = 2
if a>b: print(” a is greater than b”)


Output:
a is greater than b

An example for shorthand of state else condition:

a = 4
b = 2
print(“a is greater”) if a>b else print (“b is greater”)


Output:
a is greater

Certification in Full Stack Web Development

Lambda if else in Python

When you use an if statement in a lambda function, a value is returned based on the conditional logic contained in the if-else statement.

x = lambda n: n**2 if n%2 == 0 else n**3
print(x(4))
print(x(3))

The output will be
16
27

With this, we come to the end of this module in Python Tutorial. Here, we have discussed Python conditional statements, if condition in Python, if elif Else Python, nested if statement in Python, shorthand if condition in Python, and Python shorthand if-else statement. Now, if you are interested in knowing why python is the most preferred language for data science, you can go through this blog on Python Data Science tutorial.
Meanwhile, take a look at our offer for Python Training Course and our free guide to Python Developer Interview Questions.

Course Schedule

Name Date Details
Python Course 30 Mar 2024(Sat-Sun) Weekend Batch
View Details
Python Course 06 Apr 2024(Sat-Sun) Weekend Batch
View Details
Python Course 13 Apr 2024(Sat-Sun) Weekend Batch
View Details