// Basics
Bools — The Full Picture
The idea!
Bool is small. But it's everywhere.
This is your reference. Everything in one place.
Come back here whenever you need a reminder.
What is Bool
is_valid = True
is_banned = False
print(type(is_valid)) # <class 'bool'>
Only two values. Always capitalized.
Comparison Operators
print(10 == 10) # True — equal to
print(10 != 9) # True — not equal to
print(10 > 5) # True — greater than
print(10 < 5) # False — less than
print(10 >= 10) # True — greater than or equal to
print(10 <= 9) # False — less than or equal to
Comparison operators always return a bool.
Logical Operators
# and — both must be true
print(True and True) # True
print(True and False) # False
# or — at least one must be true
print(True or False) # True
print(False or False) # False
# not — flips the value
print(not True) # False
print(not False) # True
in and not in
name = "RedHornDev"
print("Horn" in name) # True
print("horn" in name) # False — case sensitive
print("Horn" not in name) # False
print("bull" in name.lower()) # True — normalize first
Combining Conditions
age = 20
has_id = True
is_banned = False
print(age > 18 and has_id) # True
print(age > 18 and has_id and not is_banned) # True
print(age < 18 or has_id) # True
Common Mistakes
TrueandFalseare capitalized — always=assigns,==compares — never confuse them- Don't compare a bool to
True— just use the bool and— ALL must be true.or— ANY can be true- Avoid
not not— rethink the logic instead inis case sensitive — normalize before checking
What you should understand now
- bool has only two values —
TrueandFalse - Comparison operators always return a bool
and,or,notcombine and modify conditionsinchecks existence — always returns a bool
// resources
// 0 comments
// No comments yet. Be the first.
// leave a comment