Day 12 · ~14m

Python Booleans: True, False, and Comparison Operators

Comparison operators in Python return True or False. Learn how ==, >, <, and, or, not work — and why they're the backbone of every if/else you've written since Day 6.

student (curious)

So I've been writing if/else since Day 6. I know if name: and if amount > 0: work. But like... what is Python actually checking? What is the if statement testing?

teacher (encouraging)

That's the perfect question. You're about to meet the foundation of every if statement: two values called True and False. Python tests whether something is True or False, and then decides what code to run.

student (thinking)

Wait, aren't those like... numbers? In Excel, TRUE is 1 and FALSE is 0. So if I do if 1: it's the same as if True:?

teacher (amused)

They're related — True and False will act like 1 and 0 in some contexts — but they're not identical. They're actual Python types. Watch:

print(type(True))   # <class 'bool'>
print(type(1))      # <class 'int'>
print(True == 1)    # True (they're equal)
print(True is 1)    # False (they're not the same object)

True and False are booleans — a type dedicated to truth values. Every comparison you do with <, >, ==, or with and/or/not returns a boolean.

student (focused)

OK, so what's a comparison? I see amount > 0 in my code. That's comparing amount to 0. It either is or isn't greater, right?

teacher (focused)

Exactly. amount > 0 is a comparison. It asks: "Is amount greater than 0?" Python evaluates it and returns True or False. Here are the main ones:

print(5 > 3)      # True
print(5 < 3)      # False
print(5 == 5)     # True (equals)
print(5 != 3)     # True (not equals)
print(5 >= 5)     # True (greater than or equal)
print(3 <= 5)     # True (less than or equal)

Notice == — that's "equals." A single = assigns a value to a variable. Double == asks if two values are the same.

student (excited)

So if I do if amount > 0:, Python calculates whether amount > 0 is True or False, and then runs the code inside if it's True?

teacher (encouraging)

Perfect. That's exactly how if/else works. You've been doing this since Day 6 with format_sale — you checked if amount >= 5000: and elif amount >= 1000: to decide the tier. Those comparisons return True or False, and the if statement branches based on that.

student (confused)

But you said something about and and or. How do those work?

teacher (focused)

Good question. Sometimes you need to check multiple conditions. That's where and and or come in. Here's the rule:

# and: BOTH conditions must be True
if name and amount > 0:
    print("Valid sale")

# or: AT LEAST ONE condition must be True
if score >= 8 or bonus_points > 0:
    print("Pass")

# not: flip the result
if not name:
    print("Name is empty")

and means both sides must be True. or means at least one must be True. not flips True to False and False to True.

student (proud)

So when I write if len(name.strip()) > 0 and amount > 0:, I'm checking two things at once. The name has to be non-empty AND the amount has to be positive. Only if both are true does the sale pass?

teacher (serious)

That's it. You've got the mechanism. Now let's build is_valid_sale — a function that returns True only if a sale passes both checks. This function will become part of the automation. Before you add a sale to Diane's running total, you validate it first. Just like the tier logic protected you from bad data, this validates at the source.

student (excited)

OK I'm ready. Let's write it.

teacher (focused)

Here's your starter code. The function takes a name and amount. It should return True only if the name is non-empty (after strip) AND the amount is greater than 0. Use and to combine the conditions.