Web Analytics

Booleans

Beginner ~30 min read

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!

Output
Click Run to execute your code
Fun Fact: In Python, 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 to5 == 5 โ†’ True
!=Not equal to5 != 3 โ†’ True
<Less than3 < 5 โ†’ True
>Greater than5 > 3 โ†’ True
<=Less than or equal5 <= 5 โ†’ True
>=Greater than or equal5 >= 3 โ†’ True
isSame objectx is None
inMember of"a" in "abc" โ†’ True
Output
Click Run to execute your code
Chained Comparisons: Python uniquely allows 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
andTrue if BOTH are TrueTrue and True โ†’ True
orTrue if AT LEAST ONE is TrueTrue or False โ†’ True
notInverts the valuenot False โ†’ True
Output
Click Run to execute your code
Operator Precedence: 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.

Falsy Values:
  • False, None
  • Zero: 0, 0.0, 0j
  • Empty sequences: "", [], (), {}, set()

Everything else is truthy!

Output
Click Run to execute your code
Common Pattern: Use 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
Output
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: True and False (capital letters!)
  • Comparison: ==, !=, <, >, <=, >=
  • Identity: is, is not (for object identity, use with None)
  • Membership: in, not in
  • Logical: and, or, not
  • Falsy values: False, None, 0, empty sequences
  • Short-circuit: and stops at first falsy, or stops 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.