Articles → Python → Conditional Statement In Python
Conditional Statement In Python
What Are Conditional Statements?
Different Types Of Conditional Statements
- If statement
- If-else statement
- 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")
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")