Python If and Else
Posted: Wed May 29, 2024 12:55 pm
#Basic if statement (IFEL01)
#Indentation in if (code to explain error) (IFEL02)
#Elif (IFEL03)
#Else (IFEL04)
#IF And (IFEL05)
#IF Or (IFEL06)
#IF Not (IFEL07)
#Nested IF (IFEL08)
Code: Select all
#Basic if statement
a = 55
b = 200
if b > a:
print("b is greater than a")Code: Select all
#Indentation in if (code to explain error)
a = 55
b = 300
if b > a:
print("b is greater than a") # you will get an errorCode: Select all
#Elif
a = 55
b = 55
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Code: Select all
#Else (IFEL04)
a = 400
b = 55
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")Code: Select all
#IF And
a = 400
b = 55
c = 600
if a > b and c > a:
print("Both conditions are True")
Code: Select all
#IF Or
a = 400
b = 55
c = 600
if a > b or a > c:
print("At least one of the conditions is True")Code: Select all
#IF Not
a = 55
b = 400
if not a > b:
print("a is NOT greater than b")Code: Select all
#Nested IF
x = 65
if x > 15:
print("Above fifteen,")
if x > 30:
print("and also above 30!")
else:
print("but not above 20.")