All the IPython Notebooks in Python Flow Control Statements lecture series by Dr. Milaan Parmar are available @ GitHub
if-elif-else
statement¶So far, we have presented a Boolean option for conditional statements, with each if statement evaluating to either True
or False
. In Python, the if-elif-else
condition statement has an elif
keyword used to chain multiple conditions one after another.
if condition-1:
statement 1
elif condition-2:
stetement 2
elif condition-3:
stetement 3
...
else:
statement
The elif
is short for else if. It allows us to check for multiple expressions.
If the condition for if
is False
, it checks the condition of the next elif
block and so on.
If all the conditions are False
, the body of else
is executed.
Only one block among the several if-elif-else
blocks is executed according to the condition.
The if
block can have only one else
block. But it can have multiple elif
blocks.
# Example 1:
'''In this program, we check if the number is positive or negative or zero and
display an appropriate message'''
num = 0
# Try these two variations as well:
# num = 0
# num = -4.5
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Zero
Explanation:
When variable num
is positive, Positive number
is printed.
If num
is equal to 0, Zero
is printed.
If num
is negative, Negative number
is printed.
# Example 2:
num1, num2 = 5, 5
if(num1 > num2):
print("num1 is greater than num2")
elif(num1 == num2):
print("num1 is equal to num2")
else:
print("num1 is less than num2")
num1 is equal to num2
# Example 3:
x = 10
y = 12
if x > y:
print("x>y")
elif x < y:
print("x<y")
else:
print("x=y")
x<y
# Example 4:
grade = 96
if grade >= 90:
print("A grade")
elif grade >=80:
print("B grade")
elif grade >=70:
print("C grade")
elif grade >= 65:
print("D grade")
else:
print("Failing grade")
A grade
# Example 5:
def user_check(choice):
if choice == 1:
print("Admin")
elif choice == 2:
print("Editor")
elif choice == 3:
print("Guest")
else:
print("Wrong entry")
user_check(1) # Admin
user_check(2) # Editor
user_check(3) # Guest
user_check(4) # Wrong entry
Admin Editor Guest Wrong entry
We can avoid writing nested condition by using logical operator and
.
a = 0
if a > 0 and a % 2 == 0:
print('A is an even and positive integer')
elif a > 0 and a % 2 != 0:
print('A is a positive integer')
elif a == 0:
print('A is zero')
else:
print('A is negative')
A is zero
user = 'Arthur'
access_level = 3
if user == 'admin' or access_level >= 4:
print('Access granted!')
else:
print('Access denied!')
Access denied!
num_1
is greater than num_2
return num_1
is greater than num_2
, if num_1
is less num_2
return num_1
is smaller than num_2
, else num_1
is equal to num_2
. Output:Enter number one: 9
Enter number two: 6
9 is greater than 6
80-100, A
70-89, B
60-69, C
50-59, D
0-49, F
Check if the season is Autumn, Winter, Spring or Summer.
The following list contains some fruits:
('That fruit already exist in the list')
fruits = ['banana', 'orange', 'mango', 'pear']
person={
'first_name': 'Milaan',
'last_name': 'Parmar',
'age': 96,
'country': 'Finland',
'is_marred': True,
'skills': ['Python', 'Matlab', 'R', 'C', 'C++'],
'address': {
'street': 'Space street',
'zipcode': '02210'
}
}
* Check if the person dictionary has skills key, if so print out the middle skill in the skills list.
* Check if the person dictionary has skills key, if so check if the person has 'Python' skill and print out the result.
* If a person skills has only Python and Matlab, print('He knows machine learning'), if the person skills has Python, and R print('He knows statistics'), if the person skills has C, and C++, Print('He knows software development'), else print('unknown title') - for more accurate results more conditions can be nested!
* If the person is married and if he lives in Finland, print the information in the following format:
Milaan Parmar lives in Finland. He is married.