Courses/Python Mastery/Module 3: Variables & Data Types
Module 3 · Lesson 415 minBeginner

Booleans and Comparison Operators

Lesson goal
True/False logic — the foundation of every decision your code makes.

Booleans and Comparison Operators

Every decision your code will ever make — every if, every loop, every button — boils down to one tiny type with only two possible values: True and False. Understand booleans and control flow becomes easy next module.

The type with two values

code
is_raining = True
is_sunny = False

print(type(is_raining))    # <class 'bool'>

True and False are keywords — capital first letter, and they're not strings. "True" (in quotes) is just text; True is the actual boolean value.

Comparison operators produce booleans

You rarely write True by hand — you *generate* booleans by comparing things:

code
score = 85

print(score == 100)     # False  (equal to — TWO equals signs!)
print(score != 100)     # True   (not equal)
print(score > 80)       # True   (greater than)
print(score >= 85)      # True   (greater or equal)
print(score < 80)       # False
print(score <= 85)      # True

The single most important = vs == lesson

This is the #1 beginner confusion in ALL of programming, so let's kill it now:

SymbolNameMeaningExample
=assignment"put the right side into the left"score = 85
==comparison"is the left side equal to the right?"score == 85 → True

One equals stores. Two equals asks. Say it out loud until it's automatic.

Comparing strings works too

code
print("apple" == "apple")     # True
print("Apple" == "apple")     # False! — capitalization matters
print("b" > "a")              # True — alphabetical order

That second one causes real bugs: "Yes" == "yes" is False. When comparing user input, normalize first: answer.lower() == "yes".

Combining booleans: and, or, not

code
age = 20
has_id = True

print(age >= 18 and has_id)     # True — BOTH must be true
print(age < 18 or has_id)       # True — at least ONE true
print(not has_id)               # False — flips the value
andornot
True only if both TrueTrue if at least one TrueFlips the value

Real-world shape: if age >= 18 and has_ticket: — "let them in only if both hold."

Booleans are secretly numbers

A fun true fact: True is 1 and False is 0:

code
print(True + True)        # 2
print(False * 100)        # 0

Which enables an elegant trick — counting matches:

code
answers = [True, False, True, True]
print(sum(answers))       # 3 — True counts as 1!

Common Errors & Fixes

  • `SyntaxError: invalid syntax` on `if score = 85:` — you used assignment instead of comparison. Inside if, use ==.
  • "My comparison always says False" — comparing a string to a number: "5" == 5 is False. Convert types first.
  • `TypeError` comparing different types"apple" > 5 confuses Python. Compare like with like.

  • ✅ Checkpoint

  • Difference between = and ==? *(Assignment vs comparison)*
  • What does 5 != 5 print? *(False — "not equal" is false when they ARE equal)*
  • "Yes" == "yes"? *(False — case matters)*
  • True and False? True or False? not True? *(False, True, False)*
  • Next: type conversion — the lesson that explains the input() trap you've been warned about twice already.