Conditional Statements in Python

Conditional Statements in Python

Conditional Statements in Python

Conditional statements allow you to execute certain parts of your code based on whether a condition is True or False. They are fundamental in controlling the flow of a program. In Python, there are three primary types of conditional statements: if, elif, and else.


1. The if Statement

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

Syntax:


if condition:
    # code to execute if condition is True

Example:


x = 10
if x > 5:
    print("x is greater than 5")

Output:


x is greater than 5

In this example, the condition x > 5 is True, so the code inside the if block is executed, and the message is printed.


2. The else Statement

The else statement is used to execute a block of code when the if condition is False.

Syntax:


if condition:
    # code to execute if condition is True
else:
    # code to execute if condition is False

Example:


x = 3
if x > 5:
    print("x is greater than 5")
else:
    print("x is not greater than 5")

Output:


x is not greater than 5

In this case, the condition x > 5 is False, so the code inside the else block is executed.


3. The elif Statement

The elif (short for "else if") statement allows you to check multiple conditions. If the first if condition is False, Python checks the elif condition. You can have multiple elif statements to check different conditions.

Syntax:


if condition1:
    # code to execute if condition1 is True
elif condition2:
    # code to execute if condition2 is True
else:
    # code to execute if none of the conditions are True

Example:


x = 8
if x > 10:
    print("x is greater than 10")
elif x > 5:
    print("x is greater than 5 but less than or equal to 10")
else:
    print("x is 5 or less")

Output:


x is greater than 5 but less than or equal to 10

Here, the condition x > 10 is False, so Python checks the elif condition x > 5, which is True, and prints the corresponding message.


4. Nested Conditional Statements

You can also nest if statements inside other if statements. This allows for more complex logic.

Example:


x = 7
if x > 5:
    if x < 10:
        print("x is between 5 and 10")
    else:
        print("x is greater than or equal to 10")
else:
    print("x is 5 or less")

Output:


x is between 5 and 10

In this example, since x is greater than 5 and less than 10, the nested if statement is executed.


5. Logical Operators in Conditional Statements

You can combine multiple conditions using logical operators: and, or, and not.

Example with and:


x = 10
y = 5
if x > 5 and y < 10:
    print("Both conditions are True")

Output:


Both conditions are True

Example with or:


x = 10
y = 15
if x > 5 or y < 10:
    print("At least one condition is True")

Output:


At least one condition is True

Example with not:


x = 3
if not x > 5:
    print("x is not greater than 5")

Output:


x is not greater than 5


Conclusion

Conditional statements are essential tools in Python that allow for the decision-making process in your programs. By using if, elif, and else, along with logical operators, you can handle various situations based on different conditions. These statements enable dynamic program behavior based on user input, data, or other conditions.

Comments