Conditional statements

The if statement is used to execute a block of code if a certain condition is true:

if x > 0:
    print("x is positive")

The else statement is used to execute a block of code if the condition in the if statement is false:

if x > 0:
    print("x is positive")
else:
    print("x is not positive")

The elif (short for "else if") statement is used to test multiple conditions in a single if statement:

if x > 0:
    print("x is positive")
elif x < 0:
    print("x is negative")
else:
    print("x is zero")
Comments