Google News
logo
Python Program to The elif statement
In the following example of Python program that demonstrates how to use the elif statement :
Program :
# Using the elif statement
x = 0

if x > 0:
    print("x is positive")
elif x < 0:
    print("x is negative")
else:
    print("x is zero")
Output :
x is zero
In this program, we use the `if` statement to check the value of the variable `x`. If `x` is greater than `0`, the program will execute the code in the first `if` block, which prints the message "x is positive".

If `x` is not greater than `0`, the program will check the second condition using the `elif` statement. If `x` is less than `0`, the program will execute the code in the `elif` block, which prints the message "x is negative". If `x` is neither greater nor less than `0`, the program will execute the code in the `else` block, which prints the message "x is zero".

You can use the `elif` statement to check additional conditions after the initial `if` statement. This allows you to perform different actions based on multiple possible conditions. You can also have multiple `elif` statements to check for even more conditions.