Professional Documents
Culture Documents
STUDY MATERIAL: 05
The if statement
The if statement is used to test a particular condition and if the condition is true, it
executes a block of code known as if-block. The condition of if statement can be
any valid logical expression which can be either evaluated to true or false.
if condition 1:
#statement to execute
if condition:
#block of statements
else:
#another block of statements (else-
block)
Python Programming Sem 5 EC 2022
The elif statement
The elif statement enables user to check multiple conditions and execute the
specific block of statements depending upon the true condition among more than
conditions.
One can have any number of elif statements in program depending upon
requirements. Using elif is optional
if condition 1:
# block of statements
elif condition 2:
# block of statements
elif condition 3:
# block of statements
else:
# block of statements
Nested if Statement
Nested if statements enable us to use if - else statement inside an outer if
statement.
if condition 1:
# block of statements
if condition 2: # if condition 1 is true, it will check condition 2
# block of statements
else : # if condition 2 is false, it will execute else block of inner if
# block of statements
else: # if condition 1 is false, it will execute else block of outer if
# block of statements
Python Programming Sem 5 EC 2022
# Write a program to calculate the electricity bill
unit = int(input("Enter number of units: "))
bill = 0
if unit <=150:
print("bill amount is : ", bill)
elif unit >150 and unit <=350:
print("bill amount is : ", (unit - 150)*5)
elif unit > 350:
print("bill amount is : INR ", 1000 +(unit -350)*7)
# write a python program to check an alphabet is vowel or consonant_method 1
ch = input("Enter an alphbet: ")
if ch in ('a', 'e' , 'i' , 'o' , 'u', 'A', 'E', 'I' , 'O' , 'U'):
print(ch, " is a vowel")
else:
print(ch, " is a consonant")
#write a python program to check an alphabet is vowel or consonant_ method 2
import string
l1 = {'a', 'e' , 'i' , 'o' , 'u', 'A', 'E', 'I' , 'O' , 'U'}
l2 = set(string.ascii_lowercase)
l3 = set(string.ascii_uppercase)
ch1 = input("Enter an alphbet: ")
if ch1 not in (l2 or l3):
print("enter a valid alphabet")
elif ch1 in (l2 or l3):
if ch1 in l1:
print(ch1, " is a vowel")
else:
print(ch1, " is a consonant")
#write a python program to display grades according to marks between 0 to 100:
m1 = int(input('enter ur marks between 0 to 100: '))
if m1 >100 or m1 < 0:
print(“ enter marks between 0 to 100”)
elif m1 <100:
if m1 > 85:
print("garde is AA")
elif m1 >= 75 and m1 <85:
print("garde is AB")
elif m1 >= 70 and m1 <75:
print("grade is BB")
elif m1>=65 and m1 <70:
print("grade is BC")
elif m1>=60 and m1 <65:
print("grade is CC")
elif m1>=45 and m1 <60:
print("grade is DD")
else:
print('grade is FF')