if…elif..else:
Example 1
the marks obtained by a student in 3
subjects are input through the keyboard.
the students get a division as per the
following rules:
75-100 distinction, 60--74 first
class,50-59:2nd class,40--49:3rd class
#m,p,c=int(input("enter m,p,c
marks"))#errr
m,p,c=(int(x) for x in input("enter
m,p,c marks:").split())
avg=m+p+c/3;
if avg>=75:
print("u passed in distinction %= ",avg)
elif avg>=60:
print("U passed in First class %=",avg)
elif avg>=50:
print("U passed in 2nd class %=",avg)
elif avg>=40:
print("U passed in 3rd class %=",avg)
else:
print("Failed,%=",avg)
output:
enter m,p,c marks:20 10 34
U passed in 3rd class %=
41.333333333333336
>>>
Definition:
When we want to test multiple
conditions and execute statements depending on those conditions. "if..elif..else
statement" is useful.
Syntax:
if
condition1:
Statement
elif
condition2:
Statement2
elif
conditon3:
Statement3
….
else
Default statement.
Based on the condition the corresponding
action will be executed.
Explanation: condition 1 is
True: statement 1 is executed
Remaining statements are not
executed.
If condition 2 is True:
only statement 2 is executed
If condition 3 is True:
only statement 3 is executed
Ex2:
#Largest of two nos
a=int(input("enter 1st no"))
b=int(input("enter 2nd no"))
#Logic
if a>b:
print("a is big")
else:
print("b is big")
"""
output
enter 1st no2
enter 2nd no3
b is big
>>> """
Ex3:
#find the Largest of 3 nos
a=int(input("Enter 1st
no"))
b=int(input("enter 2nd
no"))
c=int(input("enter 3rd
no"))
#Logic
if a>b and a>c:
print("Big no is a=",a)
elif b>c:
print("Big no is b=",b)
else:
print("Big no is c=",c)
"""
Note1:
Enter 1st no 10
enter 2nd no 20
enter 3rd no 30
Note2:
Enter 1st no 40
enter 2nd no 20
enter 3rd no 5
Big no is a= 40
>>>
"""
Related video: https://www.youtube.com/watch?v=VozU5n3ouHM&feature=youtu.be
Next Topic: Loops in
PythonPrevious Topic: if else statement
No comments:
Post a Comment