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

if x > 0:
    print("x is positive")
elif x < 0:
    print("x is negative")
else:
    print("x is zero")
Output :
x is positive
In the above 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 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 `if` statement to check any condition and execute code based on whether the condition is true or false. You can also use logical operators such as `and`, `or`, and `not` to combine multiple conditions.