xxxxxxxxxx
number = 9
if number < 5: # Condition 1
print("Less than 5") # Code 1
elif number < 10: # Condition 2
print("Less than 10") # Code 2
else: # If Condition 1 and 2 are not true
print("Not less than 5 or 10") # Code 3
xxxxxxxxxx
var_one = input("Give me a number: ")
var_one = int(var_one)
var_two = input("Give me another number: ")
var_two = int(var_two)
if var_one > var_two:
print(f"First number, {var_one} is bigger")
elif var_one < var_two:
print(f"Second number, {var_two} is bigger")
else:
print(f"Both numbers are the same : {var_one}")
The if-else statement handles two sides of the same condition: True and False. This works very well if we’re working with a problem that only has two outcomes.
However, in programming, it isn’t always a True or False scenario, and a problem can have multiple outcomes.
This is where the if-elif-else statement shines. It is the most comprehensive conditional statement because it allows us to create multiple conditions easily.
The elif stands for else if, indicating that if the previous condition fails, try this one.