Python if-else Statement

The if-else statement in Python is a control flow statement that allows you to execute different blocks of code based on certain conditions. It is fundamental for decision-making in programming. This guide covers various forms of the if-else statement, including basic if, if-else, elif, and nested if-else statements.

1. Basic if Statement

The basic if statement evaluates a condition and executes the block of code if the condition is true.

Syntax

if condition:
    # code to execute if condition is true

Example

age = 18

if age >= 18:
    print("You are eligible to vote.")

Output

You are eligible to vote.

2. if-else Statement

The if-else statement evaluates a condition and executes one block of code if the condition is true and another block of code if the condition is false.

Syntax

if condition:
    # code to execute if condition is true
else:
    # code to execute if condition is false

Example

age = 16

if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote.")

Output

You are not eligible to vote.

3. if-elif-else Statement

The if-elif-else statement evaluates multiple conditions sequentially and executes the corresponding block of code for the first condition that is true. If none of the conditions are true, it executes the else block.

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

score = 85

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
elif score >= 60:
    print("Grade: D")
else:
    print("Grade: F")

Output

Grade: B

4. Nested if Statements

Nested if statements are if statements within another if or else block. This allows for more complex decision-making.

Syntax

if condition1:
    # code to execute if condition1 is true
    if condition2:
        # code to execute if condition2 is also true
    else:
        # code to execute if condition2 is false
else:
    # code to execute if condition1 is false

Example

age = 20
citizenship = "USA"

if age >= 18:
    if citizenship == "USA":
        print("You are eligible to vote in the USA.")
    else:
        print("You are not a citizen of the USA.")
else:
    print("You are not eligible to vote.")

Output

You are eligible to vote in the USA.

5. Complex Example

Combining if, elif, else, and nested if statements to create complex decision-making structures.

Example

age = 20
citizenship = "USA"
residence = "California"

if age >= 18:
    if citizenship == "USA":
        if residence == "California":
            print("You are eligible to vote in California.")
        else:
            print("You are eligible to vote in the USA, but not in California.")
    else:
        print("You are not a citizen of the USA.")
else:
    print("You are not eligible to vote.")

Output

You are eligible to vote in California.

Conclusion

The if-else statement is a powerful control flow tool in Python that allows you to execute different blocks of code based on conditions. Understanding how to use if, if-else, elif, and nested if-else statements will enable you to implement complex decision-making logic in your programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top