if elif else in Python
if-elif-else in Python
When we need to construct a chain of if statement(s) then ‘elif’ clause can be used instead of ‘else’.
In the syntax of if..elif..else mentioned above, condition-1 is tested if it is true then statements-block1 is executed, otherwise the control checks condition-2, if it is true statementsblock2 is executed and even if it fails statements-block n mentioned in else part is executed.
‘elif’ clause combines if..else-if..else statements to one if..elif…else. ‘elif’ can be considered to be abbreviation of ‘else if’. In an ‘if’ statement there is no limit of ‘elif’ clause that can be used, but an ‘else’ clause if used should be placed at the end.
Example : #Program to illustrate the use of nested if statement
m1=int (input(“Enter mark in first subject : ”))
m2=int (input(“Enter mark in second subject : ”))
print (“Grade : A”)
elif avg>=70 and avg<80:
print (“Grade : B”)
elif avg>=60 and avg<70:
print (“Grade : C”)
elif avg>=50 and avg<60:
print (“Grade : D”)
print (“Grade : E”)
Enter mark in first subject : 34
Enter mark in second subject : 78
Grade : D
Output 2 :
Enter mark in first subject : 67
Example : #Program to illustrate the use of ‘in’ and ‘not in’ in if statement
ch=input (“Enter a character :”)
# to check if the letter is vowel
if ch in (‘a’, ‘A’, ‘e’, ‘E’, ‘i’, ‘I’, ‘o’, ‘O’, ‘u’, ‘U’):
print (ch,’ is a vowel’)
# to check if the letter typed is not ‘a’ or ‘b’ or ‘c’
if ch not in (‘a’, ’b’, ’c’):
print (ch,’ the letter is not a/b/c’)
Enter a character :e
e is a vowel
Enter a character :x
x the letter is not a/b/c
Python : If elif else in Python Program List
WAP to read todays date (only date Part) from user. Then display how many days are left in the current month.
WAP to compute the result when two numbers and one operator is given by user.
WAP to input a digit and print it in words.
Write a program to input any choice and to implement the following.
1. Area of square
2. Area of rectangle
3. Area of triangle
write a program to print one of the words negative, zero or positive, according to whether variable x is less than zero, zero, or greater than zero, respectively.
Write Python program to calculate Electricity Bill
Write Python program to input three number and print in ascending order
Write a Python program to check character is alphabetic character or not
Write a program to input length of three sides of a triangle. Then check if these sides will form a triangle or not.
(Rule is: a+b>c;b+c>a;c+a>b)
Write a program to input 3 sides of a triangle and print whether it is an equilateral, scalene or isosceles triangle.