Booleans
Booleans are the simplest data type - they can only be True or False.
Yet they're fundamental to programming, controlling decisions, loops,
and logic in every program you write.
Boolean Values
Python's boolean type (bool) has exactly two values: True and False.
Note the capital letters - true and false won't work!
Click Run to execute your code
bool is actually a subclass of int.
That's why True == 1 and False == 0, and you can use them in arithmetic!
Comparison Operators
Comparison operators compare values and return booleans. They're essential for conditions and filtering.
| Operator | Description | Example |
|---|---|---|
== | Equal to | 5 == 5 โ True |
!= | Not equal to | 5 != 3 โ True |
< | Less than | 3 < 5 โ True |
> | Greater than | 5 > 3 โ True |
<= | Less than or equal | 5 <= 5 โ True |
>= | Greater than or equal | 5 >= 3 โ True |
is | Same object | x is None |
in | Member of | "a" in "abc" โ True |
Click Run to execute your code
10 < x < 20 instead
of 10 < x and x < 20. Use this feature - it's cleaner and more Pythonic!
Logical Operators
Logical operators combine boolean expressions. Python uses English words: and, or, not.
| Operator | Description | Example |
|---|---|---|
and | True if BOTH are True | True and True โ True |
or | True if AT LEAST ONE is True | True or False โ True |
not | Inverts the value | not False โ True |
Click Run to execute your code
not > and > or.
Use parentheses to make your intent clear: (a and b) or c vs a and (b or c).
Truthy and Falsy Values
In Python, any value can be used in a boolean context. Values that act like False are
called falsy; values that act like True are called truthy.
False,None- Zero:
0,0.0,0j - Empty sequences:
"",[],(),{},set()
Everything else is truthy!
Click Run to execute your code
or for default values: name = user_input or "Anonymous".
If user_input is empty/falsy, name becomes "Anonymous".
Common Mistakes
1. Using = instead of ==
# Wrong - this is assignment!
if x = 5: # SyntaxError!
# Correct - comparison
if x == 5:
print("x is 5")
2. Using 'is' for value comparison
a = 1000
b = 1000
# Wrong - compares object identity
if a is b: # May be False!
# Correct - compares values
if a == b: # True
print("a equals b")
# 'is' is correct for None
if x is None: # Correct way to check None
3. Confusing 'and' short-circuit behavior
# 'and' returns first falsy value, or last value
result = "" and "hello" # Returns "", not False!
result = "hi" and "hello" # Returns "hello", not True!
# If you need actual boolean, use bool()
result = bool("" and "hello") # False
4. Checking empty containers wrong way
items = []
# Works, but not Pythonic
if len(items) == 0:
print("empty")
# Pythonic way - use truthy/falsy
if not items:
print("empty")
if items: # Check if not empty
print("has items")
Exercise: Boolean Logic Practice
Task: Predict the result of each boolean expression, then run to verify.
Skills tested:
- Comparison operators
- Logical operators (and, or, not)
- Truthy/falsy understanding
- Short-circuit evaluation
Click Run to execute your code
Show Answers
age = 17
score = 85
has_permission = True
name = ""
items = [1, 2, 3]
# 1. age >= 18 โ False (17 is not >= 18)
# 2. score > 80 and score < 90 โ True (85 is between 80 and 90)
# 3. age < 18 or has_permission โ True (17 < 18 is True)
# 4. not has_permission โ False (not True = False)
# 5. bool(name) โ False (empty string is falsy)
# 6. bool(items) โ True (non-empty list is truthy)
# 7. 10 < age < 20 โ True (17 is between 10 and 20)
# 8. name or "Anonymous" โ "Anonymous" (empty string is falsy)
# 9. items and items[0] โ 1 (items is truthy, returns items[0])
# 10. not name and has_permission โ True (not "" is True, and True = True)
Summary
- Boolean values:
TrueandFalse(capital letters!) - Comparison:
==,!=,<,>,<=,>= - Identity:
is,is not(for object identity, use withNone) - Membership:
in,not in - Logical:
and,or,not - Falsy values:
False,None,0, empty sequences - Short-circuit:
andstops at first falsy,orstops at first truthy
What's Next?
In the next lesson, we'll explore type conversion - how to convert between Python's data types like int, float, str, and bool.
Enjoying these tutorials?