Posted December 15, 2022 by up2120129
if-else statements use True and False to determine whether a requirement has been met and then produce an output based on the result. The if keyword checks the requirements are met and then produce an output for example:
Input:
a = 20
b = 30
if b > a:
print("b is greater than a")
output:
b is greater than a
The elif keyword will try another condition if the previous conditions were not met, for example:
Input:
a = 20
b = 20
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Output:
a and b are equal
The else keyword catches anything which isn't caught by the preceding conditions for example:
Input:
a = 50
b = 20
print("b is greater than a")
elif a == b:
print("a and b are equal")
else
print("a is greater than b")
Output:
a is greater than b