#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
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 = True

Important: 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: False

Not equal to (!=)

code.py
name = "John"
result = name != "Mike"
print(result)
# Shows: True

Greater than (>)

code.py
score = 85
result = score > 80
print(result)
# Shows: True

result = score > 90
print(result)
# Shows: False

Less than (<)

code.py
age = 16
result = age < 18
print(result)
# Shows: True

Greater or equal (>=)

code.py
score = 80
result = score >= 80
print(result)
# Shows: True

Less or equal (<=)

code.py
age = 18
result = age <= 18
print(result)
# Shows: True

Combining 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: True

Practice Examples

Example 1: Age check

code.py
age = 20
is_adult = age >= 18
print("Is adult:", is_adult)
# Shows: Is adult: True

Example 2: Password check

code.py
password = "secret123"
is_correct = password == "secret123"
print("Password correct:", is_correct)
# Shows: Password correct: True

Example 3: Exam pass/fail

code.py
score = 75
passing_score = 60

passed = score >= passing_score
print("Passed exam:", passed)
# Shows: Passed exam: True

Example 4: Store open

code.py
hour = 14  # 2 PM
is_open = hour >= 9 and hour <= 18
print("Store open:", is_open)
# Shows: Store open: True

Using 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: True

Example 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: True

Common 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 equal

Combining:

code.py
and   # Both must be True
or    # At least one True
not   # Opposite

Summary

  • 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!