Python
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
48: Conditional Statements: if, elif, else
Think of a conditional statement like a bouncer at the door of a club. The bouncer has a specific set of rules they have to follow before letting someone in. First, they check if you're on the VIP list. If you are, you walk right in. If you're not, they don't just send you home immediately—they check if you have a standard ticket. If that's true, you're in. If neither of those is true? Well, you're staying on the sidewalk.
In Python, we do the exact same thing. We tell the computer to evaluate a condition (a question that results in True or False) and then execute a specific block of code based on that answer. If the first condition is met, Python runs that block and skips everything else in that sequence. If not, it moves to the next check.
The Primary Gatekeeper
The if statement is your first line of defense. It's the VIP list. If the condition is True, the code indented underneath it runs. If it's False, Python simply ignores that block and moves on.
temperature = 30
if temperature > 25:
print("It's a scorcher! Turn on the AC.")
Notice the colon and the indentation. I can't stress this enough: in Python, that whitespace isn't just for looks. It's how the language knows which lines of code "belong" to that conditional check.
Handling the Middle Ground
Usually, life isn't just "Yes" or "No." Often, there's a "Maybe" or a "Wait, let me check something else." That's where elif (short for "else if") comes in. It allows you to chain multiple checks together. The key thing to remember is that Python checks these in order. As soon as it finds one that is True, it executes it and exits the entire structure.
temperature = 15
if temperature > 25:
print("Turn on the AC.")
elif temperature < 10:
print("Grab a heavy coat.")
elif temperature < 20:
print("A light sweater should do it.")
In the example above, since the temperature is 15, it fails the first check, fails the second, but hits the third. It prints the sweater message and then stops. It doesn't matter if there are more elif statements below it; once a condition is met, the search is over.
The Catch-All Safety Net
What happens if none of your conditions are met? Without an else statement, your program would just move past the block and do nothing. The else block is your "none of the above" option. It doesn't take a condition because it's simply what happens when every other check has failed.
temperature = 22
if temperature > 25:
print("Turn on the AC.")
elif temperature < 18:
print("Turn on the heater.")
else:
print("The temperature is perfect. Do nothing.")
I like to think of else as a safety net. It ensures that no matter what input your program receives, there is always a defined outcome. It prevents the program from just falling through the cracks.
📋 Practical Task
Building a Movie Ticket Pricing Engine
You've been hired to write the logic for a cinema's automated kiosk. The ticket price varies based on the customer's age. Your task is to create a script that determines the final price based on these rules:
- If the customer is under 12 years old, the price is $7.
- If the customer is 65 years or older, the price is $9.
- For everyone else, the price is $12.
Write a program that initializes an age variable and uses if, elif, and else to print the correct ticket price. Test your code with three different ages to ensure all three price brackets are working correctly.
There are no comments for now.