🕯️ Magic Note
In Python, True and False are written with capital first letters. true and false (lowercase) are not valid. This is a common beginner mistake. Remember: capital T and capital F.
Python
# Direct assignment
is_logged_in = True
is_closed = False
is_magical = True
# From comparisons
x = 10
y = 20
is_greater = x > y # False
is_equal = x == y # False
is_less = x < y # True
# From other expressions
has_items = bool([]) # False (empty list is falsy)
has_name = bool(“hello”) # True (non-empty string is truthy)
| Operator | Meaning | Example (x=5, y=10) | Result |
|---|---|---|---|
| == | Equal to | x == y | False |
| != | Not equal to | x != y | True |
| > | Greater than | x > y | False |
| < | Less than | x < y | True |
| >= | Greater than or equal to | x >= y | False |
| <= | Less than or equal to | x <= y | True |
Python
# Comparison operators in action
age = 25
print(age == 25) # True
print(age != 30) # True
print(age > 18) # True
print(age < 21) # False
print(age >= 25) # True
print(age <= 20) # False
# Compare strings (lexicographical order)
print(“apple” < “banana”) # True (‘a’ comes before ‘b’)
print(“cat” == “Cat”) # False (case sensitive)
| Operator | What It Does | Example (a=True, b=False) | Result |
|---|---|---|---|
| and | True only if BOTH are True | a and b | False |
| or | True if AT LEAST ONE is True | a or b | True |
| not | Reverses the value (True becomes False) | not a | False |
Python
is_weekend = True
has_time = False
# AND – both must be True
can_rest = is_weekend and has_time
print(can_rest) # False (has_time is False)
# OR – at least one must be True
can_go_out = is_weekend or has_time
print(can_go_out) # True (is_weekend is True)
# NOT – reverses the value
is_weekday = not is_weekend
print(is_weekday) # False
🕯️ Magic Note
The and and or operators short-circuit. For and, if the first value is False, Python does not even evaluate the second. For or, if the first value is True, Python skips the second. This can improve performance and prevent errors.
Python
# Short-circuit example
def risky_operation():
print(“This won’t run if short-circuited”)
return True
result = False and risky_operation() # risky_operation() never runs
print(result) # False
result = True or risky_operation() # risky_operation() never runs
print(result) # True
| Falsy Values (Act like False) | Truthy Values (Act like True) |
|---|---|
| None | Any non-None value |
| False | True |
| 0 (zero as int or float) | Any non-zero number (1, -1, 3.14, etc.) |
| “” (empty string) | Any non-empty string (“hello”, “0”, “False”) |
| [] (empty list) | Any non-empty list ([1, 2, 3]) |
| { } (empty dictionary) | Any non-empty dictionary ({“a”: 1}) |
| set() (empty set) | Any non-empty set ({1, 2, 3}) |
| () (empty tuple) | Any non-empty tuple ((1, 2)) |
Python
# Truthy and falsy in action
name = “”
if name:
print(f”Hello, {name}”)
else:
print(“Name is empty”) # This runs because empty string is falsy
count = 5
if count:
print(“We have items”) # This runs because 5 is truthy
items = []
if not items:
print(“The list is empty”) # This runs because empty list is falsy
Python
age = 18
has_permission = True
# Simple condition
if age >= 18:
print(“You are an adult”)
# Condition with and
if age >= 18 and has_permission:
print(“You can enter”)
# Condition with or
if age >= 18 or has_permission:
print(“At least one condition is true”)
# Condition with not
if not has_permission:
print(“Permission denied”)
Python
counter = 0
is_running = True
# Loop until a condition becomes False
while counter < 5:
print(f”Count: {counter}”)
counter += 1
# Using a boolean flag
attempts = 0
while is_running and attempts < 3:
print(“Trying…”)
attempts += 1
if attempts == 2:
is_running = False # This will end the loop next check
Python
print(bool(0)) # False
print(bool(42)) # True
print(bool(“”)) # False
print(bool(“hello”)) # True
print(bool([])) # False
print(bool([1,2])) # True
print(bool(None)) # False
print(bool(True)) # True
Python
# Booleans as numbers
print(True + True) # 2 (1 + 1)
print(True + False) # 1 (1 + 0)
print(False * 10) # 0
print(True – 1) # 0
# Counting True values in a list
results = [True, False, True, True, False]
print(sum(results)) # 3 (number of True values)
# But be careful – this is a quirk, not a feature to rely on heavily
- Writing true or false (lowercase) instead of True or False (NameError)
- Using = instead of == in conditions: if x = 5: (SyntaxError)
- Forgetting truthiness: if my_list == True instead of if my_list
- Comparing to True or False unnecessarily: if is_ready == True should be if is_ready
- Confusing and / or logic with everyday English
- What are the two boolean values in Python?
- What is the difference between = and ==?
- Which values are considered falsy in Python? Name at least four.
- What is the output of bool([]) and bool(“False”)?
- Write an if statement that checks if a number is between 10 and 20.
- What does short-circuiting mean for and and or?
⚡ Whisper
True and False. Two simple words. Two opposite whispers. Yet every decision your program makes, from the simplest to the most complex, comes down to choosing between them. Is the door open? Is the key correct? Is the time right? Each question becomes a boolean. Each boolean leads to a path. The program walks forward, choosing left or right, up or down, action or silence, all guided by these tiny torches of truth. Learn to love them. They are not just data. They are the breath of logic. The pulse of choice. The whisper that says “this way” or “not this way.” Without them, your code cannot decide. With them, it becomes alive with purpose.