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

if x > 0:
    print("x is positive")
else:
    print("x is not positive")
Output :
x is not positive
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 `if` block, which prints the message "x is positive".

If `x` is not greater than `0`, the program will execute the code in the `else` block, which prints the message "x is not positive".

You can use the `else` statement to perform an action when the initial condition in the `if` statement is false. This allows you to have an alternative action to take when a condition is not met.