Conditional Statement

A conditional statement in Python is used to execute different blocks of code based on certain conditions. It allows the program to make decisions and execute code selectively. Conditional statements evaluate expressions and, based on whether they are True or False, execute different code blocks.

Syntax: if

if condition:
        # Code to execute if the condition is True

Syntax: if..else

if condition:
    # Code to execute if the condition is True
else:
    # Code to execute if none of the above conditions are True

Syntax: if..elif..else

Performs a bitwise XOR operation between two integers. Each bit in the result is set to 1 if the corresponding bits of the operands are different, otherwise it is set to 0.

if condition:
    # Code to execute if the condition is True
elif another_condition:
    # Code to execute if the second condition is True
else:
    # Code to execute if none of the above conditions are True

Python program that demonstrates the use of if..elif..else:

# Define a variable
number = 15

# Conditional statements
if number > 20:
    print("The number is greater than 20.")
elif number == 20:
    print("The number is exactly 20.")
else:
    print("The number is less than 20.")      
What Are People Saying About Us

Read These Life Changing Stories From People Just Like You!