If Statement š«:
Another name for an if-then statement is a conditional statement.it is use to print the true or false statement
For Example:
a = 33
b = 200
if b > a:
print("b is greater than a")
b = 200
if b > a:
print("b is greater than a")
Output:
b is greater than a
If - Else Statement š«:
The if-else statement is used to execute both the true part and the false part of a given condition. If the condition is true, the if block code is executed and if the condition is false, the else block code is executed.
For Example:
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Output:
b is not greater than a
Elif Statement š«:
The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition" .Else evaluates a single statement before closing the loop. Elif allows you to evaluate multiple statements before closing the loop.
For Example:
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
elif a < b:
print("a is greater than b")
print("a is greater than b")
Output:
a and b are equal
Program in which use of all if, elif and else:
a= input("Write 'square'or'rectangle'or'circle'or'Triangle'or'Ovel'or'all' of which you want to print: ")if a=="square": print("\t\t\t SQUARE") print("\t\t\t ________________") print("\t\t\t| |") print("\t\t\t| |") print("\t\t\t| |") print("\t\t\t| SQUARE |") print("\t\t\t| |") print("\t\t\t| |") print("\t\t\t|________________|\n")elif a=="rectangle": print("\t\t\t RECTANGlE") print("\t\t\t ______________________________") print("\t\t\t| |") print("\t\t\t| |") print("\t\t\t| RECTANGlE |") print("\t\t\t| |") print("\t\t\t|______________________________|")elif a=="Ovel": print("\t\t\tOvel") print("\t\t\t * * * ") print("\t\t\t * * ") print("\t\t\t * Ovel * ") print("\t\t\t * * ") print("\t\t\t * * ") print("\t\t\t * * * ")elif a=="circle": print("\t\t\tCIRCLE") print("\t\t\t * * * ") print("\t\t\t * * ") print("\t\t\t * CIRCLE * ") print("\t\t\t * * ") print("\t\t\t * * ") print("\t\t\t * * * ")elif a=="Triangle": print("\t\t\tTRIANGLE") print("\t\t\t /\ ") print("\t\t\t / \ ") print("\t\t\t / \ ") print("\t\t\t / \ ") print("\t\t\t /TRIANGLE\ ") print("\t\t\t /__________\ ")else: print("please write with the correct spelling ".upper())
a= input("Write 'square'or'rectangle'or'circle'or'Triangle'or'Ovel'or'all' of which you want to print: ")
if a=="square":
print("\t\t\t SQUARE")
print("\t\t\t ________________")
print("\t\t\t| |")
print("\t\t\t| |")
print("\t\t\t| |")
print("\t\t\t| SQUARE |")
print("\t\t\t| |")
print("\t\t\t| |")
print("\t\t\t|________________|\n")
elif a=="rectangle":
print("\t\t\t RECTANGlE")
print("\t\t\t ______________________________")
print("\t\t\t| |")
print("\t\t\t| |")
print("\t\t\t| RECTANGlE |")
print("\t\t\t| |")
print("\t\t\t|______________________________|")
elif a=="Ovel":
print("\t\t\tOvel")
print("\t\t\t * * * ")
print("\t\t\t * * ")
print("\t\t\t * Ovel * ")
print("\t\t\t * * ")
print("\t\t\t * * ")
print("\t\t\t * * * ")
elif a=="circle":
print("\t\t\tCIRCLE")
print("\t\t\t * * * ")
print("\t\t\t * * ")
print("\t\t\t * CIRCLE * ")
print("\t\t\t * * ")
print("\t\t\t * * ")
print("\t\t\t * * * ")
elif a=="Triangle":
print("\t\t\tTRIANGLE")
print("\t\t\t /\ ")
print("\t\t\t / \ ")
print("\t\t\t / \ ")
print("\t\t\t / \ ")
print("\t\t\t /TRIANGLE\ ")
print("\t\t\t /__________\ ")
else:
print("please write with the correct spelling ".upper())
Nested Ifš:
var = 100
if var < 200:
print "Expression value is less than 200"
if var == 150:
print "Which is 150"
elif var == 100:
print "Which is 100"
elif var == 50:
print "Which is 50"
elif var < 50:
print "Expression value is less than 50"
else:
print "Could not find true expression"
print "Good bye!"
output:
Expression value is less than 200
Which is 100
Good bye!