Python — Booleans
What are Booleans?
Booleans represent one of two values: True or False:
is_active = True
is_deleted = False
Note the capital T and F — Python booleans are capitalized.
Creating Booleans
# Direct values
a = True
b = False
# From comparisons
x = 5 > 3 # True
y = 10 == 5 # False
z = 5 != 3 # True
# From function returns
is_even = lambda n: n % 2 == 0
is_even(4) # True
Comparison Operators
These return booleans:
| Operator | Meaning | Example |
|---|---|---|
== | Equal | 5 == 5 → True |
!= | Not equal | 5 != 3 → True |
> | Greater than | 5 > 3 → True |
< | Less than | 5 < 3 → False |
>= | Greater or equal | 5 >= 5 → True |
<= | Less or equal | 5 <= 3 → False |
x = 10
print(x > 5) # True
print(x == 10) # True
print(x != 5) # True
Logical Operators
| Operator | Meaning | Example |
|---|---|---|
and | Both must be True | True and False → False |
or | At least one True | True or False → True |
not | Flips the value | not True → False |
and
age = 25
has_id = True
if age >= 21 and has_id:
print("Entry allowed")
or
is_weekend = False
is_holiday = True
if is_weekend or is_holiday:
print("Day off!")
not
is_logged_in = False
if not is_logged_in:
print("Please log in")
Truthiness and Falsiness
Python treats certain values as True or False:
Falsy Values
False
0
0.0
0j # complex zero
"" # empty string
[] # empty list
() # empty tuple
{} # empty dict
set() # empty set
None
Truthy Values
True
1 # any non-zero number
-1
"hello" # any non-empty string
[1, 2] # non-empty list
{"a": 1} # non-empty dict
Using Truthiness
# Check if list has items
items = [1, 2, 3]
if items:
print("List is not empty")
# Check if string is not empty
name = ""
if not name:
print("Name is empty")
# Check if number is non-zero
count = 0
if count:
print("Count is non-zero")
bool() Function
Convert any value to boolean:
bool(0) # False
bool("") # False
bool(None) # False
bool([]) # False
bool(1) # True
bool("hello") # True
bool([1, 2]) # True
bool({"a": 1}) # True
Short-Circuit Evaluation
Python stops evaluating as soon as the result is determined:
# and — returns first falsy value
result = "" and "hello" # "" (empty string is falsy)
result = "hi" and "hello" # "hello"
# or — returns first truthy value
result = "" or "default" # "default"
result = "hello" or "world" # "hello"
# Practical use: default values
name = user_name or "Anonymous"
Ternary Conditional
# condition_if_true if condition else condition_if_false
status = "adult" if age >= 18 else "minor"
# Nested ternary (avoid for readability)
grade = "A" if score >= 90 else "B" if score >= 80 else "C"
Common Patterns
# Check membership
if "apple" in fruits:
print("Found apple")
# Guard clauses
if not user:
return redirect("/login")
# Boolean flag
is_running = True
while is_running:
# game loop
if should_quit:
is_running = False
# Toggle
lights_on = not lights_on
Best Practices
- Use
isforNonecomparisons:if x is None - Use
==for value comparisons:if x == 5 - Don't compare booleans to
True/Falseexplicitly- Bad:
if is_active == True - Good:
if is_active
- Bad:
- Use truthiness for checking emptiness:
if items:notif len(items) > 0 - Use
notfor negation:if not items:
Mini Practice
- Create variables and compare them with all comparison operators
- Write a condition using
and,or, andnot - Test truthiness of different values with
bool() - Use short-circuit evaluation to set default values
- Write a ternary expression to classify a number
Up Next
Next: Strings →
Related Topics
Frequently Asked Questions about Booleans
What is Booleans in Python?
Booleans is a fundamental concept in Python. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Booleans?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Booleans.
Why is Booleans important in Python?
Booleans is essential for Python development. Understanding this concept will help you write better code and solve real-world problems more effectively.