Skip to Content
Course content

49: Comparison and Logical Operators

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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 the guest_list (True/False).
  • The guest must NOT be on the banned_list (True/False).

Requirements:

  1. Create four variables: age, member_card, guest_list, and banned_list. Give them values that would allow the person to enter.
  2. Create a single boolean variable called allowed_entry.
  3. Assign a value to allowed_entry using a combination of comparison and logical operators that enforces all three rules above.
  4. Print the final value of allowed_entry.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.