DridhOn

If Else Statements in Python with Example

Share on facebook
Share on twitter
Share on pinterest
Share on linkedin
Share on whatsapp

Python Tutorial: IF ELSE Statements with Examples

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

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.

Python Conditional Statements

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. Decision structures evaluate multiple expressions, which produce TRUE or FALSE as the outcome. You need to determine which action to take and which statements to execute if the outcome is TRUE or FALSE otherwise. 

Following is the general form of a typical decision-making structure found in most the programming languages

python else if

> Syntax of IF Statements in Python

The IF statement is similar to that of other languages. The if statement contains a logical expression using which the data is compared and a decision is made based on the result of the comparison.

Following flowchart explains the working of if  in Python:

 

Syntax:

if expression:
statement(s)

If the boolean expression evaluates to TRUE, then the block of statements inside the if statement is executed. In Python, statements in a block are uniformly indented after the : symbol. If a boolean expression evaluates to FALSE, then the first set of code after the end of the block is 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.

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

Input:

# # Example :
a=int(input('Enter the Number = '))
if a==14:
print('Correct if Block')

OutPut:

#OutPut
Enter the Number = 14
Correct if Block

> Syntax of IF ELSE Statements 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 statement only if the condition is false, then we use if else in Python.

Following flowchart explains the working of if else in Python:

python else if

Syntax:

if expression:
statement(s)
else:
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. 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 start with indentation.

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

Input:

# # Example :
a=int(input('Enter the Number = '))
if a==14:
print('Correct if Block')
else:
print("wrong else Block")

OutPut:

#OutPut1
Enter the Number = 14
Correct if Block

#OutPut2
Enter the Number = 11
Wrong else Block

> Syntax of Nested IF Statements in Python

As the name suggests, nested if 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.

Following flow chart depicts the working of nested if statements.

else if python

Syntax:

if expression1:
statement(s)
if expression2:
statement(s)
else:
statement(s)
else:
statement(s)

There may be a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested if construct. In a nested if construct, you can have an if…..else construct inside another if……else construct.

Input:

# Example

a=int(input('Enter the number= '))
if a>=11:
print("a is lesser")
if a%
2==0:
print('Even Number')
else:
print("Odd Number")
else:
print("a is greater")

 

OutPut:

#OutPut1
Enter the Number = 14
a is lesser

Even Number
#OutPut2
Enter the Number = 15
a is lesser
Odd Number
#OutPut3
Enter the Number = 10
a is greater

 

> Syntax of ELIF Statements 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 the first if 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.

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 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 the last else block is executed.

Following flowchart depicts the working of if elif else statements:

 
python if

Syntax:

if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)

Core Python does not provide switch or case statements as in other languages, but we can use if..elif…statements to simulate switch case as follows

Input:

# Example

a=input('Enter the Continent=')
if a=='Asia':
print('Russia, India, Srilanka, Nepal')
elif a=='Europe':
print('Italy, London, UK')
elif a=='Australia':
print('Australia, Newzeland')
elif a=='Africa':
print('South Africa, Egypt, Nigeria')
else:
print('No continent is available')

OutPut:

#OutPut1
Enter the Continent = Asia
Russia, India, Srilanka, Nepal


 

Questions:

Assignment
#
# 1.Take values of length and breadth of a rectangle from user and check if it is square or not.
#
# 2.Take two int values from user and print greatest among them.
#
# 3.A shop will give discount of 10% if the cost of purchased quantity is more than 1000.
# a. Ask user for quantity
# b. Suppose, one unit will cost 100.
# c. Judge and print total cost for user.
#
# 4.A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years.
# a. Ask user for their salary and year of service and print the net bonus amount.
#
# 5.A school has following rules for grading system:
# a. Below 25 - F
# b. 25 to 45 - E
# c. 45 to 50 - D
# d. 50 to 60 - C
# e. 60 to 80 - B
# f. Above 80 – A
# a. Ask user to enter marks and print the corresponding grade.
#
# 6.Take input of age of 3 people by user and determine oldest and youngest among them.
#
# 7.Write a program to print absolute value of a number entered by user. E.g.-
# INPUT: 1 OUTPUT: 1
# INPUT: -1 OUTPUT: 1
#
# 8.A student will not be allowed to sit in exam if his/her attendence is less than 75%.
# Take following input from user
# a. Number of classes held
# b. Number of classes attended.
# c. percentage of class attended
# e. Is student is allowed to sit in exam or not.
#
# 9.Modify the above question to allow student to sit if he/she has medical cause. Ask user if he/she has medical cause or not ( 'Y' or 'N' ) and print accordingly
#
#
# 10. Wirte the calculator pgm where it takes 3 inputs from user.
#
# 11. Please take one user as input and give output as a server name.
#
# 12. username and password valdiation program. Input user name = string & Password is string.

Related Posts

python certification training

Loops Statement in Python

What is Loops? A loop statement in python allows users to accomplish an individual statement or group of statements multiple times. In Python we have 2 types of loops: FOR

Read More »
Scroll to Top