Introduction

Conditional statements are fundamental in programming, allowing you to execute different blocks of code based on certain conditions. In Python, the primary conditional statements are if, elif, and else.

Key Concepts

  1. if Statement: Executes a block of code if a specified condition is true.
  2. elif Statement: Stands for "else if" and allows you to check multiple expressions for true and execute a block of code as soon as one of the conditions is true.
  3. else Statement: Executes a block of code if none of the preceding conditions are true.

Syntax

if Statement

if condition:
    # block of code to be executed if the condition is true

if-else Statement

if condition:
    # block of code to be executed if the condition is true
else:
    # block of code to be executed if the condition is false

if-elif-else Statement

if condition1:
    # block of code to be executed if condition1 is true
elif condition2:
    # block of code to be executed if condition2 is true
else:
    # block of code to be executed if none of the conditions are true

Practical Examples

Example 1: Basic if Statement

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

Explanation: This code checks if the variable age is greater than or equal to 18. If true, it prints "You are an adult."

Example 2: if-else Statement

age = 16
if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

Explanation: This code checks if age is greater than or equal to 18. If true, it prints "You are an adult." Otherwise, it prints "You are a minor."

Example 3: if-elif-else Statement

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

Explanation: This code checks multiple conditions to determine the grade based on the score. It prints the corresponding grade based on the first true condition.

Practical Exercises

Exercise 1: Determine Even or Odd

Write a program that checks if a number is even or odd.

number = int(input("Enter a number: "))
if number % 2 == 0:
    print(f"{number} is even.")
else:
    print(f"{number} is odd.")

Exercise 2: Check Voting Eligibility

Write a program that checks if a person is eligible to vote based on their age.

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

Exercise 3: Grading System

Write a program that assigns a grade based on a score.

score = int(input("Enter your score: "))
if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
else:
    print("Grade: F")

Common Mistakes and Tips

  • Indentation: Python relies on indentation to define blocks of code. Ensure that the code inside the if, elif, and else statements is properly indented.
  • Boolean Expressions: The conditions in if, elif, and else statements must evaluate to True or False.
  • Order of Conditions: In an if-elif-else chain, conditions are checked in order. Once a condition is true, the corresponding block of code is executed, and the rest of the conditions are ignored.

Conclusion

Conditional statements are essential for controlling the flow of a program based on different conditions. By mastering if, elif, and else statements, you can create more dynamic and responsive programs. In the next topic, we will explore loops, which allow you to execute a block of code multiple times.

Python Programming Course

Module 1: Introduction to Python

Module 2: Control Structures

Module 3: Functions and Modules

Module 4: Data Structures

Module 5: Object-Oriented Programming

Module 6: File Handling

Module 7: Error Handling and Exceptions

Module 8: Advanced Topics

Module 9: Testing and Debugging

Module 10: Web Development with Python

Module 11: Data Science with Python

Module 12: Final Project

© Copyright 2024. All rights reserved