Conditional Statement
Conditional statement are used to check for the condition and based on the result, it will execute the statement
If Statement
Check the condition, if its true then execute the statement which is available inside the block
#IF statement
age = 25
#Check the age is greater tha 18
if age>18:
print("Eligible to Vote")
Output
Eligible to Vote
If - Else Statement
Check the condition, if its true then execute the statement which is available inside the if block, if its false then execute the statement which is available inside the else block
#IF - ELSE statement
age = 14
#Check the age is greater tha 18
if age>18:
print("Eligible to Vote")
else:
print("InEligible to Vote")
Output
InEligible to Vote
If - Elif - Else Statement
With the help of elif keyword, we can check the multiple else condition in python
#IF - ELIF - ELSE statement
mark = 75
#Checking the mark with different grade levels
if mark>90:
print("A Grade")
elif mark>80:
print("B Grade")
elif mark>70:
print("C Grade")
elif mark>60:
print("D Grade")
elif mark>50:
print("E Grade")
else:
print("F Grade")
Output
C Grade
Nested If Else Statements
Inside the conditional statements, we can nest multiple if and else condition like the below example
#Nested IF-ELSE statements
mark = 75
#Checking the mark with different grade levels
if mark>50:
if mark>60:
if mark>70:
if mark>80:
if mark>90:
print("A Grade")
else:
print("B Grade")
else:
print("C Grade")
else:
print("D Grade")
else:
print("E Grade")
else:
print("F Grade")
Output
C Grade
Shorthand If Statement
If we have only one line inside if block, then we can use shorthand method like below to reduce the coding lines
#Shorthand IF statement
age = 25
#Check the age is greater tha 18
if age>18: print("Eligible to Vote")
Output
Eligible to Vote
Shorthand Else Statement
If we have only one line inside if and else block, then we can use shorthand method like below to reduce the coding lines
#Shorthand IF-ELSE statement
age = 14
#Check the age is greater than 18
print("Eligible to Vote") if age>18 else print("InEligible")
Output
InEligible
Pass Statement
Sometimes for future implementation, we required the placeholders, Pass statement helps to achieve this, it ignores by interpreter and won't throws any error
#Pass statement
age = 14
#Interpreter ignores pass statement
if age>18:
pass
print("Age is : {}".format(age))
Output
Age is : 14