Articles → Python → Conditional statement in python

Conditional statement in python






What are conditional statements?





Different types of conditional statements











if Statement






if condition: Statements to be executed

Or

if condition:
      Statement 1
      Statement 2  




age = 25

if age > 21:
    print("you are eligible to buy liquor")
    print("This line gets executed along with previous line")
print("this line will get executed for sure even if the condition is not satisfied")    







Click to Enlarge




if-else statement






if condition:
      Statement 1
else:
      Statement 2




age = 25

if age > 21:
    print("you are eligible to buy liquor")
else:
    print("you are not eligible to buy liquor")





Click to Enlarge


elif statement






if condition 1:
      Statement 1
elif condition 2:
      Statement 2




marks = 90

if marks > 90:
    print ("You have got A grade")
elif marks > 80:
    print ("You have got B grade")
else:
    print ("You have got C grade")





Click to Enlarge


Nested If




age = 21
isIndianCitizen = True


if age > 18:
    if isIndianCitizen == True:
        print("You are eligible to vote")





Click to Enlarge


Ternary operator






age = 21
isIndianCitizen = True


if age > 18:
    if isIndianCitizen == True:
        print("You are eligible to vote")
else:
    print("not eligible to vote")




age = 21
isIndianCitizen = True


print("You are eligible to vote"  if age > 18 and isIndianCitizen == True else "not eligible to vote")





Posted By  -  Karan Gupta
 
Posted On  -  Monday, February 18, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250