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
- if Statement: Executes a block of code if a specified condition is true.
- 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.
- else Statement: Executes a block of code if none of the preceding conditions are true.
Syntax
if Statement
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
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
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
, andelse
statements is properly indented. - Boolean Expressions: The conditions in
if
,elif
, andelse
statements must evaluate toTrue
orFalse
. - 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
- Introduction to Python
- Setting Up the Development Environment
- Python Syntax and Basic Data Types
- Variables and Constants
- Basic Input and Output
Module 2: Control Structures
Module 3: Functions and Modules
- Defining Functions
- Function Arguments
- Lambda Functions
- Modules and Packages
- Standard Library Overview
Module 4: Data Structures
Module 5: Object-Oriented Programming
Module 6: File Handling
Module 7: Error Handling and Exceptions
Module 8: Advanced Topics
- Decorators
- Generators
- Context Managers
- Concurrency: Threads and Processes
- Asyncio for Asynchronous Programming
Module 9: Testing and Debugging
- Introduction to Testing
- Unit Testing with unittest
- Test-Driven Development
- Debugging Techniques
- Using pdb for Debugging
Module 10: Web Development with Python
- Introduction to Web Development
- Flask Framework Basics
- Building REST APIs with Flask
- Introduction to Django
- Building Web Applications with Django
Module 11: Data Science with Python
- Introduction to Data Science
- NumPy for Numerical Computing
- Pandas for Data Manipulation
- Matplotlib for Data Visualization
- Introduction to Machine Learning with scikit-learn