If...Else Statements in Python

The if...else statement is one of the most fundamental tools for decision-making in Python. It allows you to control the flow of your program based on conditions that evaluate to either True or False. Depending on the outcome, you can execute different blocks of code.

Basic If Statement

An if statement checks a condition, and if the condition is true, the code within the if block is executed. If the condition is false, the block of code is skipped.

Python
x = 10

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

# Output: x is greater than 5

In this example, since x is greater than 5, the message "x is greater than 5" is printed.

If...Else Statement

Sometimes, you want to execute one block of code if a condition is true and another block if the condition is false. This is where the else clause comes in.

Python
x = 10

if x > 15:
    print("x is greater than 15")
else:
    print("x is not greater than 15")

# Output: x is not greater than 15

Here, since x is not greater than 15, the else block is executed, and "x is not greater than 15" is printed.

Elif Statement

The elif (short for "else if") statement allows you to check multiple conditions sequentially. Once a condition is found to be true, the corresponding block of code is executed, and the rest of the conditions are skipped.

Python
x = 10

if x > 15:
    print("x is greater than 15")
elif x == 10:
    print("x is 10")
else:
    print("x is less than 10")

# Output: x is 10

In this example, since x == 10 is true, the program prints "x is 10" and skips the other conditions.

Nested If Statements

You can place an if statement inside another if statement. This is known as nesting. Nested if statements are useful when you need to make multiple decisions before executing a block of code.

Python
x = 20
y = 30

if x > 10:
    if y > 20:
        print("x is greater than 10 and y is greater than 20")

# Output: x is greater than 10 and y is greater than 20

In this example, both conditions x > 10 and y > 20 are true, so the nested block of code is executed.

Multiple Conditions Using Logical Operators

You can check multiple conditions in a single if statement using logical operators like and, or, and not.

Python
x = 10
y = 5
z = 15

if x > 5 and y < 10 and z == 15:
    print("All conditions are true")

# Output: All conditions are true

In this example, the if statement checks whether all three conditions are true. Since they are, the message "All conditions are true" is printed.

Ternary Operator

Python also supports a concise way to write if...else statements, known as the ternary operator or conditional expression. It allows you to assign a value based on a condition in a single line of code.

Python
x = 10
result = "Greater than 5" if x > 5 else "5 or less"
print(result)

# Output: Greater than 5

In this example, result is assigned "Greater than 5" because x > 5 is true.

Using If...Else with Lists and Strings

The if statement can be used with lists and strings to check for membership or to compare values.

Python
fruits = ["apple", "banana", "cherry"]
if "apple" in fruits:
    print("Apple is in the list")

# Output: Apple is in the list

string = "Hello, World!"
if "Hello" in string:
    print("The string contains "Hello"")

# Output: The string contains "Hello"

Here, the in keyword is used to check whether "apple" is in the list and whether "Hello" is in the string.

Practical Example: User Authentication

Let's look at a practical example of using if...else statements in a simple user authentication scenario:

Python
username = "admin"
password = "password123"

entered_username = input("Enter your username: ")
entered_password = input("Enter your password: ")

if entered_username == username and entered_password == password:
    print("Access granted")
else:
    print("Access denied")

# Output will vary based on user input

This example checks whether the entered username and password match the predefined values. If both match, access is granted; otherwise, it is denied.

Conclusion

The if...else statement is a powerful tool in Python that allows you to control the flow of your program based on conditions. By mastering if, elif, and else statements, you can build complex decision-making logic in your programs. Remember to also explore nested conditions and the use of logical operators for even more control.