bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

Control Flow Statements – If, Else

Like other 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 the code based on the conditions used.

In this module we will learn about if else control flow statements. Following is the list of all the topics that we will cover, in case you need to jump to a specific one.

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

Watch this video on Control flow in Python.

 

Python conditionals and If 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. In python, decision making is implemented using if…else statements. The conditional logic in Python is primarily based on the “if … else” structure.

Starting from if statements, it is the most basic decision-making statement. It simply decides whether a particular code block will be executed or not on the basis if 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.

Following flowchart explains the working of if statements in Python:

if statements
Syntax of if statement:

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 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 executed.

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

Let’s see an example of implementation of 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

Learn Python in 16 hrs from experts

Python if else statements

if statements tell 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 statement but only if the condition is false, then we use if…else statements.

Following flowchart explains the working of if else statements.

Python if else statement
Syntax of if…else statement:

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 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 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 start with indentation.

Let’s see an example of implementation of 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

Wish to Learn Python? Click Here

if elif else statements

Here, the elif stands for else if. 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 the first if condition is true then, same as before in previous if and if else statements, the program will execute the body of if statement. Otherwise the program will go to the elif block (else if) which basically checks for another if statement. Again, if the condition is true the program will execute the body of 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 else block.

if elif else ladder:

When there is more than just two if conditions in if elis 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 turns 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 last else block is executed.

Following flowchart depicts the working of if elif else statements:

if elif else

Syntax of if elif else statement

if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else

You can put as many elif statements as you program requires before the last else statements, making it an if elif else ladder.
Let’s see the following example of 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 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 program goes to the next elif statement. Same thing happens here. Since all the conditions were false, 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”.

Become Python Certified in 16 hrs.
CLICK HERE

Python nested if statements

As the name suggests, nested if statements are nested inside of another if statement. That means that 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.

Following flow chart depicts the working of nested if statements.

Nested if

Syntax of nested if:

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 to understand better

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, since a is 20 the program enters in the first if statement. Then it checks the nested if statement and executes it as true and prints 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 if block.

 Short hand if and if else

Short hand if and if else is nothing but a way to write the if statements in one line when you have only one statement to execute in if block and else block.
Short hand If example:

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

Output:

a is greater than b

Short hand if else example:

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

Output:

a is greater

With this we come to an end of this module. Next module highlights while loops in Python. Stay tuned! Meanwhile, you can take a look at our offer for Python Training Course and a free guide to Python Interview Questions.

Previous Next

Download Interview Questions asked by top MNCs in 2019?

"0 Responses on Python If, Else Statements"

Leave a Message

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

Sign Up or Login to view the Free Python If, Else Statements.