Booleans and Comparisons

Python 3.14 · ✓ verified by execution on 2026-07-21

Booleans and Comparisons

Python has a special type for representing truth called a boolean. A boolean can only be one of two values: True or False — always capitalized, exactly like that.

You use booleans constantly without thinking about it: “is the user logged in?”, “is the cart empty?”, “did the payment succeed?”. Each of those questions has a yes/no answer, and in Python that answer is a boolean.

python
is_active = True
is_logged_in = False
print(is_active)
print(is_logged_in)
Output
True
False

Truthiness

Values that aren’t booleans can still be treated as True or False when Python needs a yes/no answer. This is called truthiness. The rule is simple: empty things are falsy, non-empty things are truthy. An empty string '' and an empty list [] both count as False; anything with content counts as True.

python
print(bool(''))
print(bool([]))
Output
False
False

Comparisons

Comparing two values gives you a boolean result. Python has six comparison operators: <, <=, >, >=, == (equal), and != (not equal).

python
print(5 > 3)
print(2 == 1)
Output
True
False

A Python trick you won’t find in most languages: you can chain comparisons to read like ordinary math. Instead of writing 0 < x and x < 10, you can write it the way you’d say it out loud:

python
x = 5
print(0 < x < 10)
Output
True

Logical Operators

To combine conditions, Python gives you three words: and, or, and not. They read almost like English — “logged in and active”, “admin or owner”, “not empty”.

These operators are short-circuit: Python stops as soon as it knows the answer. With and, if the first value is falsy the whole thing is already False, so the second value is never even evaluated. That’s why the next example prints False instead of crashing on the 1 / 0:

python
print(False and (1 / 0 == 0))
Output
False

Watch Out

A classic first-day mistake is writing true or TRUE. Python is case-sensitive here — only the capitalized True and False are booleans. Lowercase true will raise a NameError because Python looks for a variable by that name and doesn’t find one.

See It Step by Step

The visualizer below steps through False and (1 / 0 == 0) so you can watch execution stop after the first value — the 1 / 0 is never reached, which is exactly why no ZeroDivisionError appears.

python · visualize
print(False and (1 / 0 == 0))

Check yourself

What is the correct way to write a boolean 'true' in Python?

Reveal answer

True — In Python, boolean values must be capitalized: True and False.

What does the expression bool('') evaluate to?

Reveal answer

False — Empty collections, including empty strings, evaluate to False in a boolean context.

Which operator checks if two values are equal?

Reveal answer

== — The == operator checks for equality. The = operator is used for assignment.

Challenges

Challenge 1 +10 XP

Given variables height and age, print True if height is at least 120 and age is at least 10.

python
  • Test 1 — expects "True\n"
Need a hint? (−25% XP)

Think about the conditions.

Show solution (0 XP)
height = 130
age = 11
print(height >= 120 and age >= 10)

Challenge 2 +10 XP

Given a variable score, print True if the score is between 0 and 100 inclusive.

python
  • Test 1 — expects "True\n"
Need a hint? (−25% XP)

Think about the conditions.

Show solution (0 XP)
score = 85
print(0 <= score <= 100)