HomeCourseModule 03 › Booleans and comparisons

Booleans and comparisons

Module 03 · Variables and Data Types6 min readBeginner

What you'll learn

  • Use comparison operators (==, !=, >, <, >=, <=)
  • Combine conditions with and/or/not
  • Recognise 'truthy' and 'falsy' values

True and False — capitalised

is_open = True
is_closed = False
type(is_open)   # <class 'bool'>

Two values. Lowercase true/false won't work — Python is fussy about the capitals.

Comparison operators

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal to5 != 5False
>Greater than10 > 3True
<Less than10 < 3False
>=Greater or equal5 >= 5True
<=Less or equal5 <= 4False
⚠️ One equals vs two equals
= stores a value. == tests for equality. x = 5 is "store 5 in x". x == 5 is "is x equal to 5?". Mixing them up is the most common beginner bug.

Combining: and, or, not

age = 25
income = 80000

(age > 18) and (income > 50000)   # True
(age < 18) or (income > 100000)   # False
not (age < 18)                    # True

"Truthy" and "falsy"

Python treats some non-boolean values as if they were True or False:

name = ""
if name:
    print(f"Hi, {name}")
else:
    print("No name provided.")
# Output: No name provided.

The in operator — bonus useful

"alice" in ["alice", "bob", "carol"]   # True
"@" in "alice@example.com"             # True

Often replaces a chunky IF + OR formula in Excel.

Key takeaways

  • True and False with a capital letter.
  • == compares; = assigns. Don't mix them up.
  • Combine conditions with and, or, not.
  • Empty containers ("", [], 0, None) count as False.

Quick check

What does each of these print?

print(10 == 10.0)
print(10 == "10")
print("a" in "alpha")
print(0 or 5)
Answers
  1. True — Python is happy comparing 10 (int) and 10.0 (float).
  2. False — a number is never equal to a string of that number.
  3. True — "a" appears in "alpha".
  4. 5or returns the first truthy value.
📹 Video walkthrough
A video walkthrough of this lesson will be embedded here. Until then, the written walkthrough above mirrors what the video will cover step-for-step.