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.
bool typeMeet 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:
That's it. No maybes, no probablys — just True and False. (Note the capital T and F — Python is picky.)
The Six Comparison Operators
You create booleans by comparing values. Python gives you six comparison operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | True |
| != | Not equal to | 5 != 3 | True |
| < | Less than | 3 < 7 | True |
| > | Greater than | 3 > 7 | False |
| <= | Less than or equal | 5 <= 5 | True |
| >= | Greater than or equal | 3 >= 7 | False |
Comparing Different Types
Python can compare numbers across types (int vs float) without any issues:
Comparing Strings
Strings are compared lexicographically (like dictionary order), and comparisons are case-sensitive:
Storing Boolean Results
Comparisons produce booleans, and you can store them in variables just like any other value:
Naming convention: boolean variables often start with is_, has_, or can_ — this makes your code read like English: "is adult?"
Challenges
Challenge 1: Comparison Lab
Predict what each comparison evaluates to, then store the results.
== 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
Use comparison operators to determine eligibility for different things based on age.
• 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 2:
can_vote = age >= 18 — now do the same pattern for the other two!
Challenge 3: Password Validator
Check whether a password meets length and match requirements — storing each check as a boolean.
• 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
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
Use string comparison operators to determine alphabetical ordering.
• 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
"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