4 min read
Data Types III - Booleans
Learn about True and False values in Python
What You'll Learn
- What Boolean values are
- Using True and False
- Comparing values
- Simple yes/no logic
What are Booleans?
Booleans are simple: just True or False.
Think of them as answers to yes/no questions:
- Is it raining? True or False
- Are you 18 or older? True or False
- Is the door open? True or False
code.py
is_raining = True
is_sunny = False
has_umbrella = TrueImportant: Capital T and F!
Comparing Numbers
Comparisons give you True or False:
Equal to (==)
code.py
age = 25
result = age == 25
print(result)
# Shows: True
result = age == 30
print(result)
# Shows: FalseNot equal to (!=)
code.py
name = "John"
result = name != "Mike"
print(result)
# Shows: TrueGreater than (>)
code.py
score = 85
result = score > 80
print(result)
# Shows: True
result = score > 90
print(result)
# Shows: FalseLess than (<)
code.py
age = 16
result = age < 18
print(result)
# Shows: TrueGreater or equal (>=)
code.py
score = 80
result = score >= 80
print(result)
# Shows: TrueLess or equal (<=)
code.py
age = 18
result = age <= 18
print(result)
# Shows: TrueCombining Conditions
AND (both must be True)
code.py
age = 25
has_license = True
can_drive = age >= 18 and has_license
print(can_drive)
# Shows: True (both conditions are met)code.py
age = 16
has_license = True
can_drive = age >= 18 and has_license
print(can_drive)
# Shows: False (age is not 18 or more)OR (at least one must be True)
code.py
is_weekend = True
is_holiday = False
can_relax = is_weekend or is_holiday
print(can_relax)
# Shows: True (one condition is met)NOT (opposite)
code.py
is_raining = False
is_sunny = not is_raining
print(is_sunny)
# Shows: TruePractice Examples
Example 1: Age check
code.py
age = 20
is_adult = age >= 18
print("Is adult:", is_adult)
# Shows: Is adult: TrueExample 2: Password check
code.py
password = "secret123"
is_correct = password == "secret123"
print("Password correct:", is_correct)
# Shows: Password correct: TrueExample 3: Exam pass/fail
code.py
score = 75
passing_score = 60
passed = score >= passing_score
print("Passed exam:", passed)
# Shows: Passed exam: TrueExample 4: Store open
code.py
hour = 14 # 2 PM
is_open = hour >= 9 and hour <= 18
print("Store open:", is_open)
# Shows: Store open: TrueUsing with If Statements
code.py
age = 20
if age >= 18:
print("You can vote!")
else:
print("Too young to vote")
# Shows: You can vote!Converting to Boolean
Anything can become True or False:
code.py
# Numbers
print(bool(1)) # True
print(bool(0)) # False
print(bool(-5)) # True
# Strings
print(bool("Hello")) # True
print(bool("")) # False (empty)Real-World Examples
Example 1: Check eligibility
code.py
age = 25
income = 30000
eligible = age >= 18 and income >= 20000
print("Eligible for loan:", eligible)
# Shows: Eligible for loan: TrueExample 2: Weather decision
code.py
is_raining = False
is_cold = True
need_jacket = is_raining or is_cold
print("Need jacket:", need_jacket)
# Shows: Need jacket: TrueCommon Mistakes
Mistake 1: Using = instead of ==
code.py
age = 25
# Wrong
if age = 25: # Error!
# Correct
if age == 25: # Good!Mistake 2: Wrong capitalization
code.py
is_open = true # Wrong!
is_open = True # Correct!Quick Reference
Comparisons:
code.py
== # Equal
!= # Not equal
> # Greater than
< # Less than
>= # Greater or equal
<= # Less or equalCombining:
code.py
and # Both must be True
or # At least one True
not # OppositeSummary
- Booleans are True or False
- Use comparisons: ==, !=, >, <, >=, <=
- Combine with and, or, not
- Used for decisions in your code
- Always capital T and F
What's Next?
Now let's learn about operators to do more with our data!