Python Booleans: A Complete Beginner Tutorial

Booleans are one of the most basic — yet powerful — data types in Python. They help your programs make decisions and control the flow of logic.

1. What is a Boolean?

In Python, a Boolean represents one of two values:

  • True — the condition is correct
  • False — the condition is incorrect
a = True
b = False

print(a)  # Output: True
print(b)  # Output: False

2. Type of Boolean

Use the type() function to confirm the data type:

print(type(True))   # Output: <class 'bool'>
print(type(False))  # Output: <class 'bool'>

3. Booleans from Comparison Operators

When you compare values, Python returns True or False:

print(10 > 5)    # True
print(10 < 5)    # False
print(5 == 5)    # True
print(5 != 4)    # True
print(7 >= 7)    # True
print(3 <= 1)    # False

4. Boolean Conversion with bool()

You can convert any value to Boolean using bool():

These return True:

print(bool(123))       # True
print(bool("hello"))   # True
print(bool([1, 2]))    # True

These return False:

print(bool(0))         # False
print(bool(""))        # False
print(bool([]))        # False
print(bool(None))      # False

Rule of Thumb:

  • Empty values → False
  • Non-empty values → True

5. Logical Operators

OperatorMeaningExampleResult
andBoth conditions are trueTrue and TrueTrue
orAt least one is trueTrue or FalseTrue
notReverses the conditionnot TrueFalse

Example:

a = True
b = False

print(a and b)    # False
print(a or b)     # True
print(not a)      # False
print(not b)      # True

6. Using Booleans in if Statements

Booleans help control the flow of programs using conditions.

age = 18

if age >= 18:
    print("You can vote.")
else:
    print("You are too young to vote.")

Output:

You can vote.

7. Real Example – Login Check

username = "admin"
password = "1234"

if username == "admin" and password == "1234":
    print("Login successful")
else:
    print("Invalid credentials")

Output:

Login successful

8. Boolean in Loops

Booleans can control while loops:

count = 0

while count < 3:
    print("Count:", count)
    count += 1

Output

Count: 0
Count: 1
Count: 2

9. Custom Boolean Rules with Classes

If a class defines a __len__ method, Python uses it to evaluate its truthiness:

class Empty:
    def __len__(self):
        return 0

obj = Empty()
print(bool(obj))  # Output: False

10. Summary Table

Valuebool(value)Notes
0, 0.0FalseNumeric zero is false
1, 3.14TrueNon-zero numbers are true
""FalseEmpty string is false
"hello"TrueNon-empty string is true
[], {}FalseEmpty containers are false
[1], {x}TrueNon-empty are true
NoneFalseSpecial value for nothing

Bonus: Boolean Shortcuts

Python allows shorthand checks:

if name:  # instead of if name != ""
    print("Name is provided.")