Conditional Statements (If, Elif, Else) In Python

In this comprehensive guide, we will delve into the world of Python, focusing on Conditional Statements. This topic is perfect for beginners getting their first taste of Python’s power, as well as more experienced Python enthusiasts who wish to refresh their knowledge.


Conditional Statements (If, Elif, Else) In Python
Conditional Statements (If, Elif, Else) In Python

This guide will walk you through how if, elif, and else Python statements work, explaining concepts in easy-to-understand language and—including practical examples for better comprehension. Brace yourself for an exciting journey through the world of Python conditional statements. Let’s go!

What are Conditional Statements?

Conditional statements are a critical feature of any programming language. They provide the ability to control the flow of the code based on specific conditions. In Python, the primary conditional statements are if, elif, and else.

If Statement in Python

We use the if statement in Python to test a specific condition. If the condition is True, Python will execute the block of code under the if statement. If the condition is False, Python will ignore the if block and move on to the next section of the code.

Here is the general syntax for an if statement in Python:

if condition:
    block_of_code

Here is a basic example:

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

In this example, Python checks whether the age variable is greater than or equal to 18. As it’s true, Python executes the print statement inside the if block.

The Elif Statement

Python uses the elif (short for else if) statement to check multiple expressions for true and executes the block of code as soon as one of the conditions evaluates to true. Similar to else, the elif statement is optional. However, unlike else, for which there can be at most one statement, there can be an arbitrary number of elif statements following an if.

if condition1:
    block_of_code1
elif condition2:
    block_of_code2 

Consider an example where we evaluate a student’s grade:

score = 78

if score >= 90:
    print('Grade is A')
elif score >= 80:
    print('Grade is B')
elif score >= 70:
    print('Grade is C')

The program checks conditions from top to bottom. Once a situation satisfies, Python executes that corresponding code block and skips the rest, regardless of whether their condition might also be true.

The Else Statement

In Python, the else statement is optional and is only executed when the if condition is False.

Here is the syntax for an else statement in Python:

if condition:
    block_of_code1
else:
    block_of_code2

Consider the voting eligibility example:

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

In this case, the age is less than 18, so the condition in the if statement is False. Therefore, Python executes the code block under the else statement, printing You are not eligible to vote.

Nesting Conditional Statements

Python enables the nesting of if statements within if statements, i.e., if an action depends on a combination of conditions. Here’s an example illustrating the same:

age = 20
citizenship = 'USA'

if age >= 18:
    if citizenship == 'USA':
        print("You are eligible to vote.")
    else:
        print("Non-citizens cannot vote.")
else:
    print("You are not eligible to vote.")

In this scenario, we first check if the age is over 18. If this condition is True, a nested if checks whether the citizenship is ‘USA’. If both conditions are True, Python will print You are eligible to vote.

Conclusion

Conditional statements in Python empower programmers to control the logical flow of their programs. The application of if, elif, and else statements is limitless, ranging from small scripts to complex systems.

Understanding these statements will undoubtedly contribute massively to your Python programming skills. So keep practicing and integrating these conditional statements into your programs. If you have any questions or would like additional Python tips, don’t hesitate to reach out to us. Happy coding!

Remember, Python doesn’t believe in saying ‘No’, it believes in saying ‘Yes, if…’.

Share this article:

Leave a Comment