Conditionals in Python

Conditional statements in Python are used to execute code based on a condition. Conditions are the expressions that evaluate to True or False.

If Statement

The if statement is used to execute a block of code only if the condition is True.

if condition:
    # code block

If-Else Statement

The if-else statement is used to execute one block of code if the condition is True and another block of code if the condition is False.

if condition:
    # code block
else:
    # code block

If-Elif-Else Statement

The if-elif-else statement is used to execute one block of code among several alternatives.

if condition1:
    # code block
elif condition2:
    # code block
else:
    # code block

Example

Here's an example of using conditionals to determine if a number is positive, negative, or zero:

number = 10

if number > 0:
    print("Number is positive")
elif number < 0:
    print("Number is negative")
else:
    print("Number is zero")