Answer:
if-elif-else
Explanation:
In Python if condition is used to decide whether a statement or a block of statements is to be executed or not based on the condition, if the condition evaluates to true then the block of statements is executed and if the condition is false then it is not executed.
Sometimes we need to execute some other statement when the condition is false. For example
if (number1 <= number2)
print (number1, "is the smallest")
Lets suppose we want to print another statement if this condition evaluates to false. If the condition gets false then the following message should be displayed:
print(number 2, "is the smallest")
For this purpose else statement is used along with if statement to execute the block of code when the if condition evaluates to false.
if (number1 <= number2)
print (number1, "is the smallest")
else:
print(number 2, "is the smallest")
Now what if there are three numbers or more numbers to be compared in order to identify the smallest number from the given numbers. So in this case we have multiple options to be checked. else statement will not be enough a for else there can be at most one statement. With if elif else multiple conditions can be checked and if any of the condition evaluates to true then its block of code is executed and if none of the condition is true then the last else statement will be executed.
For example:
if (number1 <= number2) and (number1 <= number3):
print (number1, "is the smallest")
elif (number2 <= number1) and (number2 <= number3):
print (number1, "is the smallest")
else:
print (number3, "is the smallest")
In this example number1 is compared to number2 and number3. If both numbers are less than number1 then the program control goes to elif statement where number2 is compared to number1 and number3. if this condition is false too then the final else part will be printed which is that number3 is the smallest.
Take another example where there are more number of expressions to be checked.
val = 50
if (val == 40):
print ("value is 40")
elif (val== 35):
print ("value is 35")
elif (val == 25):
print ("value is 25")
elif(val==15):
print ("value is 15")
else:
print ("value is not present")
This will output the else part value is not present, because none of the condition evaluates to true.