In Python, the bool data type represents logical truth values with exactly two singleton constants: True and False (note capital T and F).
In Python, bool is actually a subclass of int, where True == 1 and False == 0.
In Python, the bool data type represents logical truth values with exactly two singleton constants: True and False (note capital T and F).
In Python, bool is actually a subclass of int, where True == 1 and False == 0.
== (equal), != (not equal), <, >, <=, >=.and (both true), or (at least one true), not (inverts boolean value).# Boolean expressions and logical operators
age = 20
has_license = True
has_insurance = False
can_drive = (age >= 18) and has_license and (not has_insurance or True)
print(f"๐ Can legally drive? {can_drive}")
# Testing Truthiness of various Python objects
test_values = [
True, False, 1, 0, -5, "", "Hello", [], [1, 2], {}, {"key": "val"}, None
]
print("
๐ Truthiness Evaluation Table:")
for val in test_values:
print(f" {repr(val):<18} -> bool(): {bool(val)}")
Avoid writing "if is_active == True:". The Pythonic way is simply "if is_active:" or "if not is_active:".
Use truthiness to validate input collections and default configurations.
def process_user_cart(items):
# Empty list [] evaluates to Falsy
if not items:
return "๐ Your shopping cart is empty! Please add items."
return f"โ
Processing {len(items)} items: {', '.join(items)}"
print(process_user_cart([]))
print(process_user_cart(["MacBook Pro", "AirPods", "USB-C Hub"]))
Yes! Because bool is a subclass of int, True + True equals 2, and sum([True, False, True]) equals 2.