Python provides the elif
statement which is used in if conditional statement. The elif statement is the short form of the else if
statement. The elif statement is used to add new conditions to the existing if statement by specifying the conditions and related code blocks to execute.
Single elif Statement Usage
We can use single elif statement to add new conditions to the existing if statement. In the following example, we check the age if it is over 18 or over 65.
age=38
if age>65:
print("Age is over 65")
elif age>18:
print("Age is over 18")
Multiple elif Statements Usage
We can use the elif statement to add multiple conditions to the existing if statement. In the following example, we use multipe elif statements in order to check different age groups like over 18 or over 12.
age=38
if age>65:
print("Age is over 65")
elif age>18:
print("Age is over 18")
elif age>12:
print("Age is over 12")