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.
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
Your output
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
Your output
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 = 5print(0 < x < 10)
Output
True
Your output
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
Your output
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 = 130age = 11print(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 = 85print(0 <= score <= 100)
Go deeper
More Buckets Will Not Save a Bad Hash Function — Hash tables are called O(1) so often that the condition gets dropped. Change the hash function here, drag the table from 4 buckets to 16, and watch the longest chain refuse to move - because memory cannot fix a hash that ignores its input.
The Same Seven Numbers, Two Trees: Why Insert Order Decides Lookup Cost — Insert seven numbers into a binary search tree in sorted order and you get a seven-level chain. Insert the same seven in a different order and you get three levels. Build both here, and watch what the shape costs on every lookup afterwards.
A Rotation Is Just Re-Hanging Three Subtrees, and the Order Never Changes — Self-balancing trees are usually taught as four case names before anyone says what a rotation does. Insert the same values with rebalancing on and off here, and watch a tree that would be seven levels deep stay at three.
List or Matrix? Guess Before You Look, and Watch the Answer Flip — Lists for sparse graphs, matrices for dense ones is true enough to repeat and too vague to use. Predict the winner here on two dials - density and question mix - and find out how often the rule of thumb is wrong.
No Sorting Algorithm Wins. Look at Your Data First. — Insertion sort is not slow and quicksort is not fast - each is the cheapest choice for some shape of input and the dearest for another. Predict the winner across five shapes here and watch a single favourite fail.