Unit 2 · Lesson 1

Booleans & Comparisons

Every decision your code makes comes down to one question: True or False? In this lesson, you'll learn to ask that question in six different ways.

Understand the bool type
Use all 6 comparison operators
Predict boolean expressions
Compare strings alphabetically

Meet the Boolean

In Unit 1, you learned about str, int, and float. Now meet the simplest type of all: bool. A boolean has exactly two possible values:

print(True) # a bool print(False) # a bool print(type(True)) # <class 'bool'>

That's it. No maybes, no probablys — just True and False. (Note the capital T and F — Python is picky.)

Why do we care? Because every if statement you'll ever write runs a boolean check behind the scenes. Mastering booleans now means mastering decisions later.

The Six Comparison Operators

You create booleans by comparing values. Python gives you six comparison operators:

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
<Less than3 < 7True
>Greater than3 > 7False
<=Less than or equal5 <= 5True
>=Greater than or equal3 >= 7False
⚠️ Common Mistake: = (single) is assignment — it stores a value. == (double) is comparison — it asks a question. Mixing these up is one of the most common Python bugs!

Comparing Different Types

Python can compare numbers across types (int vs float) without any issues:

print(5 == 5.0) # True — same value, different types print(3 != 3.0) # False — they ARE equal

Comparing Strings

Strings are compared lexicographically (like dictionary order), and comparisons are case-sensitive:

print("apple" < "banana") # True — a comes before b print("hello" == "Hello") # False — case matters! print("Zebra" < "apple") # True — uppercase letters come first
How string comparison works: Python compares characters one at a time using their Unicode values. All uppercase letters (A-Z) have lower Unicode values than lowercase letters (a-z), so "Zebra" < "apple" is True.

Storing Boolean Results

Comparisons produce booleans, and you can store them in variables just like any other value:

age = 17 is_adult = age >= 18 print(is_adult) # False print(type(is_adult)) # <class 'bool'>

Naming convention: boolean variables often start with is_, has_, or can_ — this makes your code read like English: "is adult?"

Practice Time

Challenges

🧪

Challenge 1: Comparison Lab

Guided

Predict what each comparison evaluates to, then store the results.

Instructions: For each expression, set the variable to what you think the comparison evaluates to — True or False. Don't use the comparison itself — type the boolean value directly!
Hint 1: Work through each one carefully. == checks if values are equal, != checks if they're different.
Hint 2: Remember, 3 == 3.0 compares values, not types. And strings are case-sensitive!
Hint 3: The answers are: True, True, False, False, False, True.
🎟️

Challenge 2: Eligibility Check

Guided

Use comparison operators to determine eligibility for different things based on age.

Instructions: Using the age variable, create three boolean variables using comparison expressions (not just True/False):
can_vote — True if age is 18 or older
is_teenager — True if age is 13 or older (assume under 20)
can_rent_car — True if age is 25 or older
Hint 1: Each one uses a single comparison operator. For "18 or older", think about which operator means "greater than or equal to".
Hint 2: can_vote = age >= 18 — now do the same pattern for the other two!
🔐

Challenge 3: Password Validator

Solo

Check whether a password meets length and match requirements — storing each check as a boolean.

Instructions: Given the variables below, create:
long_enough — True if the password length is >= 8 characters (use len())
passwords_match — True if password equals confirm_password
not_too_long — True if the password length is <= 20 characters
Hint 1: len(password) gives you the number of characters. Compare that number to 8 or 20.
Hint 2: For matching, just use == to compare the two password strings directly.
Hint 3: long_enough = len(password) >= 8
🔤

Challenge 4: Alphabetical Order

Stretch

Use string comparison operators to determine alphabetical ordering.

Instructions: Given two words, create boolean variables to check their order. To handle case-sensitivity, convert both to lowercase first using .lower().
word1_lower — word1 converted to lowercase
word2_lower — word2 converted to lowercase
word1_first — True if word1_lower comes before word2_lower alphabetically
same_word — True if the lowercase versions are equal
Hint 1: "Banana".lower() gives you "banana". Use the < operator to check alphabetical order.
Hint 2: Without .lower(), "Banana" < "apple" would be True (uppercase first). With it, "banana" < "apple" is False because b comes after a.
Hint 3: word1_lower = word1.lower(), then word1_first = word1_lower < word2_lower