Comparison Operators — Making True/False Decisions
Now that you can do math with arithmetic operators, it's time to ask questions. Can I compare two values? Is 10 bigger than 5? Are these two numbers the same? Comparison operators let you answer these questions. Instead of doing math and getting a number back, comparisons return True or False—answers to yes/no questions.
Think of comparison operators like a referee in a game: they look at two things, make a judgment, and give you an answer: "True" (yes, this condition is met) or "False" (no, it's not).
What It Is: Asking True/False Questions
A comparison operator evaluates whether a condition is true or false. When you write 5 > 3, you're asking Python: "Is 5 greater than 3?" Python answers: "True."
Python has six comparison operators:
==Equality — Are these values the same?!=Not equal — Are these values different?>Greater than — Is the left side bigger?<Less than — Is the left side smaller?>=Greater than or equal — Is the left side bigger or the same?<=Less than or equal — Is the left side smaller or the same?
All comparisons return a boolean value: either True or False. This is different from arithmetic operators, which return numbers. Comparisons return yes/no answers.
The Six Comparison Operators
Let's see all six in action. Each one asks a different question about two values.
Equality and Inequality: Are They the Same?
Loading Python environment...
Important distinction: In Lesson 1, we saw that = assigns a value to a variable. Here, == compares two values. One = stores; two == compare. This is a common mistake, so remember it well.
💬 AI Colearning Prompt
"Why does Python use = for assignment but == for equality checking? Compare this to other programming languages. Why is this distinction important?"
Notice that we're asking AI to explain the design choice behind the operators, not just what they do. This helps you understand Python's thinking, not memorize syntax.
Magnitude Comparisons: Which is Bigger?
Loading Python environment...
The operators >, <, >=, <= compare magnitude—which value is bigger. They answer: "Is the left side bigger, smaller, or equal?" The = in >= and <= means "or equal to," so 10 >= 10 is True (they're equal).
🎓 Expert Insight
In AI-native development, you don't memorize the difference between
>and>=. You think: "Do I want to include the equal case or not?" Then you use the operator that matches your intent. If you're uncertain, you ask AI: "Should I use > or >= here?" and let AI help you reason through the condition.
Value Equality vs. Type Equality
Here's something that surprises many beginners: Python compares values, not types.
Loading Python environment...
This is crucial: 5 == 5.0 returns True because the values are the same, even though one is int and one is float. But 5 == "5" returns False because "5" is text, not a number.
🤝 Practice Exercise
Ask your AI: "I'm confused about why 5 == 5.0 is True, but 5 == '5' is False. Explain value equality vs. type equality. When does Python care about types vs. just values in comparisons?"
Expected Outcome: You'll understand that == checks if values are the same (regardless of type in most cases), but type mismatches between completely different types (int vs. string) make them not equal. You'll see how Python's flexibility can be useful but also requires careful thinking.
Comparisons in Real Scenarios
Loading Python environment...
Why Comparisons Matter for Chapter 22
You might wonder: "Why are we learning comparisons separately from if statements?" The answer is that comparisons are building blocks. In Chapter 22, you'll learn control flow: making decisions with if statements. When you write if x > 5:, that x > 5 is a comparison operator at work. By mastering comparisons now, Chapter 22 will feel natural—you'll already understand how to ask True/False questions.
Try With AI
Ready to master comparison operators and understand why = is different from ==?
🔍 Explore Comparison Operators:
"Explain the 6 comparison operators:
==,!=,>,<,>=,<=. For each, show me 2 examples with numbers and explain the True/False result. Why does Python use == for comparison but = for assignment? Show me what happens if I accidentally write if x = 5: instead of if x == 5:"
🎯 Practice Comparison Logic:
"Create a movie ticket checker: movie_age_rating = 13, user_age = 12. Write comparisons to check: (1) if user can watch (user_age >= movie_age_rating), (2) if user is too young (user_age < movie_age_rating), (3) if ages are exactly equal (user_age == movie_age_rating). For each, predict True or False, then verify with code."
🧪 Test Type Comparison Edge Cases:
"Test these comparisons and explain which return True: (1) 5 == 5.0 (int vs. float), (2) '5' == 5 (string vs. int), (3) True == 1 (bool vs. int), (4) False == 0 (bool vs. int). Why does Python treat some cross-type comparisons as equal but not others? Show me the type() of each value."
🚀 Apply to Your Validation:
"I'm building [describe your application]. Help me write comparison logic for: age restrictions, price checks, quota limits, version compatibility, or other validation rules. For each condition, show me the correct comparison operator and explain why that operator is the right choice."