Completed
-
1. Introduction to Python
-
2. Python Basics
-
3. Working with Data Structures
-
4. Functions and Modules
-
5. Object-Oriented Programming (OOP)
-
6. File Handling
-
7. Error and Exception Handling
-
8. Python for Data Analysis
-
9. Advanced Topics in Python
-
10. Working with APIs
-
11. Python for Automation
-
12. Capstone Projects
- 13. Final Assessment and Quizzes
2.4.1 If, else, elif
In Python, conditional statements like if, elif, and else allow you to control the flow of execution based on conditions (True or False). These statements help to execute specific blocks of code when certain conditions are met.
1. if Statement
The if statement tests a condition and executes a block of code if the condition is True. If the condition is False, the code block under if is skipped.
age = 18 if age >= 18: print("You are an adult.")
- Explanation: In this example, age >= 18 is the condition. Since the condition is True (age is 18), the code inside the if block will execute, printing "You are an adult."
2. elif Statement (else if)
The elif (short for else if) statement is used to check multiple conditions. It is only executed if the previous if or elif conditions were False. You can have multiple elif statements in a program.
age = 15 if age >= 18: print("You are an adult.") elif age >= 13: print("You are a teenager.")
- Explanation: In this example, the if condition (age >= 18) is False, so it moves to the elif statement. Since the condition age >= 13 is True, it prints "You are a teenager."
3. else Statement
The else statement is used to run a block of code when none of the previous conditions (i.e., if and elif) are True. The else block is optional and is used to define a default action when all conditions fail.
age = 10 if age >= 18: print("You are an adult.") else: print("You are not an adult.")
- Explanation: Since the if condition (age >= 18) is False, the program moves to the else block and prints "You are not an adult."
4. Combining if, elif, and else
You can combine if, elif, and else to handle multiple conditions and provide specific outputs based on the given inputs. There can only be one if block, but there can be multiple elif blocks, and there can be one else block (which is optional).
age = 22 if age >= 60: print("You are a senior citizen.") elif age >= 18: print("You are an adult.") elif age >= 13: print("You are a teenager.") else: print("You are a child.")
- Explanation: The if condition checks if the person is 60 or older, which is False. It then checks if the person is 18 or older, which is True, and prints "You are an adult." If neither condition had been true, it would continue with the other elif and else blocks.
Flow of Execution:
- if: The program first checks the if condition. If it is True, the code inside the if block is executed, and the rest of the conditions are skipped.
- elif: If the if condition is False, the program checks the conditions in the elif statements, one by one, in order.
- else: If none of the conditions in if or elif are True, the program executes the code inside the else block, if it exists.
Summary:
- if: Executes code if a condition is True.
- elif: Checks additional conditions if the previous conditions were False.
- else: Executes code if all previous conditions are False.
Using these statements, you can create more dynamic programs that can make decisions based on different inputs or situations.
Commenting is not enabled on this course.