Skip to main content

How to Use if, elif, and else Statements in Python

How to Use if, elif, and else Statements in Python | Rustcode

How to Use if, elif, and else Statements in Python

Conditional statements are fundamental in Python programming. They allow your code to make decisions and execute different blocks of code based on specific conditions. In this article, you'll learn how to use if, elif, and else statements in Python, with syntax, code examples, outputs, and clear explanations.


What are Conditional Statements?

  • Definition: Conditional statements let your program choose which code to run based on whether a condition is True or False.
  • Use Cases: Decision making, branching logic, input validation, and more.
  • Benefits: Makes your code dynamic, interactive, and responsive to different situations.

01. The if Statement

The if statement executes a block of code only if a specified condition is True.

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

Output:

x is greater than 5
Explanation:
  • If the condition x > 5 is true, the code inside the if block runs.
  • If the condition is false, nothing happens.

02. The if-else Statement

The else statement provides an alternative block of code if the if condition is False.

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
Explanation:
  • If the if condition is not met, the else block runs.
  • Ensures that one of the two code blocks will always execute.

03. The if-elif-else Statement

The elif (short for "else if") allows you to check multiple conditions in sequence.

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

Output:

Grade: B
Explanation:
  • Checks each condition in order from top to bottom.
  • The first condition that is True runs its block, and the rest are skipped.
  • else is optional and runs if none of the previous conditions are met.

04. Nested if Statements

You can place one if statement inside another to check multiple levels of conditions.

num = 15
if num > 0:
    if num % 2 == 0:
        print("Positive even number")
    else:
        print("Positive odd number")
else:
    print("Non-positive number")

Output:

Positive odd number
Explanation:
  • The outer if checks if the number is positive.
  • The inner if checks if it's even or odd.
  • Nested if statements allow for more complex decision-making.

05. Comparing if, elif, and else in Python

Statement Purpose Required? How Many? Notes
if Checks a condition Yes One per block Starts the conditional chain
elif Checks more conditions No Zero or more Comes after if
else Default action No Zero or one Comes last, no condition

Conclusion

The if, elif, and else statements are the foundation of decision-making in Python. They allow your programs to react to different situations and make your code dynamic and powerful. Practice using these statements in your projects to master Python's conditional logic!

Tip: Indentation is crucial in Python! Always use consistent indentation for blocks inside if, elif, and else.

Comments