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
49: Comparison and Logical Operators
How do I actually check if two values are the same?
This is the most common place where people trip up when they're starting out. In Python, a single equals sign = is for assignment—you're telling Python "make this variable equal to this value." To compare two things, you have to use the double equals ==.
I've seen plenty of bugs caused by accidentally using = inside an if statement. If you want to see if a user's input matches a password or if a player's score hit a target, use ==. If you want to check if they don't match, use !=.
user_role = "editor"
# Checking if the role is exactly "admin"
is_admin = (user_role == "admin") # This will be False
# Checking if the role is NOT "guest"
is_not_guest = (user_role != "guest") # This will be True
How do I combine multiple conditions together?
Usually, one check isn't enough. You'll often find yourself needing to verify a few things at once using and, or, and not. Think of these as the glue for your logic.
and requires both sides to be True. or only needs one of them to be True. I like to think of or as a "safety net"—as long as one condition is met, you're good to go.
age = 22
has_ticket = True
is_vip = False
# To enter, you need to be 18+ AND have a ticket
can_enter = (age >= 18) and has_ticket
# To get a fast-pass, you can be a VIP OR have a special gold ticket
# (Let's assume we don't have a gold ticket variable here)
has_fast_pass = is_vip or False
print(can_enter) # True
print(has_fast_pass) # False
What is the not operator actually for?
The not operator is a simple flipper. It takes whatever boolean value is to its right and turns it into the opposite. While you could sometimes just change == True to == False, using not is much more readable. It reads like a sentence in English, which is exactly why Python does it this way.
I use this most often when I'm checking for "empty" states or "disabled" flags.
is_logged_in = False
if not is_logged_in:
print("Please log in to continue.")
Can I chain comparisons like I do in math class?
Yes! This is one of those "quality of life" features in Python that I really love. In many other languages, if you want to check if a number is between 10 and 20, you have to write x > 10 and x < 20. In Python, you can just chain them together.
It’s cleaner, it’s faster to write, and it’s much easier for the next person reading your code to understand at a glance.
temperature = 72
# Check if the temperature is within the "comfortable" range
is_comfortable = 65 <= temperature <= 75
print(is_comfortable) # True
📋 Practical Task
Exercise: Building a Digital Bouncer for a VIP Club
You are writing the entry logic for a high-end club. Your task is to create a script that determines if a guest is allowed to enter based on the following strict rules:
- The guest must be at least 21 years old.
- The guest must either have a
member_card(True/False) OR be on theguest_list(True/False). - The guest must NOT be on the
banned_list(True/False).
Requirements:
- Create four variables:
age,member_card,guest_list, andbanned_list. Give them values that would allow the person to enter. - Create a single boolean variable called
allowed_entry. - Assign a value to
allowed_entryusing a combination of comparison and logical operators that enforces all three rules above. - Print the final value of
allowed_entry.
There are no comments for now.