If Else If Statement in Python

Hello friends how are you, today in this blog i will teach you what is if else if statement, syntax of if else if, how if else if statement works and many programs using if else if in a very simple way. 

Let's start

If else if ladder statement

Syntax:

if  condition  :
    statement1
elif condition:
    statement2
elif condition:
    statement3
else:
    statement4
  • It is used to test the condition.
  • If executes only one condition at a time.
  • The condition which is true first from the top will execute.

no=7
if no>10:  /*false*/
    print("Hello1")
elif no>5: /*true*/
    print("Hello2")
elif no>0: /*true*/
    print("Hello3")
else:
    print("Helllo4")
/*
 ### Output ###
 Hello2
 because it executes only one condition 
 which becomes true first from top.
 */


no=-5
if no>10:
    print("Hello1")
elif no>5:
    print("Hello2")
elif no>0: 
    print("Hello3")
else:
    print("Helllo4")
/*
 ### Output ###
 Helllo4
 because  all conditions are false.
*/

percent=float(input("Enter your percentage:"))
if percent>=60:
    print("First division")
elif percent>=45:
    print("Second division")
elif percent>=33:
    print("Third division")
else:
    print("Sorry!!! You are fail.")
/*
Output
Enter your percentage:56
Second division
*/
Example 3:Find greatest value in three number


#taking user input for numbers
no1=int(input("Enter first number:"))
no2=int(input("Enter second number:"))
no3=int(input("Enter third number:"))
#applying condition on numbers
if no1>no2 and no1>no3:
    print(no1," is greatest value")
elif no2>no1 and no2>no3:
    print(no2," is greatest value")
else:
    print(no3," is greatest value")
"""
###Output###
Enter first number:78
Enter second number:85
Enter third number:54
85  is greatest value
"""


Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. 

Try to learn something new and teach something new to other. Thanks.

Post a Comment

0 Comments