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")
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")
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")
Nested If
age = 21
isIndianCitizen = True
if age > 18:
if isIndianCitizen == True:
print("You are eligible to vote")
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")