Python Conditional Statements: if, elif, else Ultimate guide

Conditional Statements in Python (if, elif, else)

1. Introduction

Understanding conditional statements in Python (if, elif, else) is essential for making decisions in a program. therefore Conditional statements allow the program to execute different blocks of code depending on conditions.

In this guide, we will explore how to use if, elif, and else in Python, real-life applications, common mistakes, and how to fix them.

Focus Keyphrase: Conditional Statements in Python (if, elif, else)

2. What are Conditional Statements?

Conditional statements help a program decide what to do next based on conditions. Python provides three main conditional statements:

  • if Statement – Runs a block of code only if a condition is true.
  • elif (else if) Statement – Checks additional conditions if the first if condition is false.
  • else Statement – Runs a block of code when all previous conditions are false.

3. Using if, elif, and else in Python

a) if Statement

Executes code if the condition is true.

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

Output:

You are eligible to vote.

b) if-else Statement

Executes different code depending on whether the condition is true or false.

age = 16
if age >= 18:
    print("You can vote.")
else:
    print("You are too young to vote.")

Output:

You are too young to vote.

c) if-elif-else Statement

Checks multiple conditions.

marks = 85

if marks >= 90:
    print("Grade: A")
elif marks >= 75:
    print("Grade: B")
elif marks >= 60:
    print("Grade: C")
else:
    print("Grade: F")

Output:

Grade: B

4. Real-Life Applications of Conditional Statements

a) Login Authentication System

username = input("Enter username: ")
password = input("Enter password: ")

if username == "admin" and password == "1234":
    print("Access granted")
else:
    print("Access denied")

b) Discount System for an Online Store

purchase_amount = float(input("Enter purchase amount: "))

if purchase_amount > 1000:
    discount = 20
elif purchase_amount > 500:
    discount = 10
else:
    discount = 5

print(f"Discount applied: {discount}%")

c) Traffic Light Simulation

light = "red"

if light == "green":
    print("Go")
elif light == "yellow":
    print("Slow down")
else:
    print("Stop")

5. Common Mistakes and How to Fix Them

Mistake 1: Incorrect Indentation

Incorrect:

age = 20
if age >= 18:
print("You are an adult.")  # IndentationError

Fix: Always use consistent indentation (4 spaces or a tab).

age = 20
if age >= 18:
    print("You are an adult.")

Mistake 2: Using = Instead of == in Conditions

Incorrect:

age = 18
if age = 18:  # SyntaxError: "=" is for assignment, not comparison
    print("You are 18.")

Fix: Use == for comparison.

if age == 18:
    print("You are 18.")

Mistake 3: Forgetting elif and Writing Multiple if Statements

Incorrect:

marks = 85

if marks >= 90:
    print("Grade: A")
if marks >= 75:
    print("Grade: B")  # Incorrect: This will also print "Grade: B"

Fix: Use elif to prevent multiple true conditions from executing.

marks = 85

if marks >= 90:
    print("Grade: A")
elif marks >= 75:
    print("Grade: B")  # Correct: Only one condition executes

Mistake 4: Not Handling else Properly

Incorrect:

age = 15
if age >= 18:
    print("You can drive.")
elif age < 18:
    print("You cannot drive.")
else:
    print("Invalid age.")  # This will never run

Fix: Ensure else handles unexpected cases.

age = int(input("Enter your age: "))

if age >= 18:
    print("You can drive.")
else:
    print("You cannot drive.")

6. Conclusion

Thus, Mastering conditional statements in Python (if, elif, else) is crucial for writing decision-making programs. Understanding these statements helps in tasks like authentication, grading systems, and automation.

Next Steps:

  • Practice if, elif, and else statements in real-life scenarios.
  • Avoid common mistakes like indentation errors and incorrect comparisons.
  • Experiment with complex conditions using and / or operators. Reference
Scroll to Top