Articles → Python → Conditional Statement In Python

Conditional Statement In Python






What Are Conditional Statements?





Different Types Of Conditional Statements




  1. If statement
  2. If-else statement
  3. elif statement

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")    






Picture showing the output of if conditional statement in python

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")




Picture showing the output of if-else conditional statement in python

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")




Picture showing the output of elif conditional statement in python

Click to Enlarge


Nested If




age = 21
isIndianCitizen = True


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




Picture showing the output of nested if conditional statement in python

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