Skip to main content

Python Boolean Data Type

A Boolean is a data type that represents only two possible values:

  • True
  • False

These are not just ordinary words—they are special reserved keywords in Python, and they always begin with a capital letter.

a = True
b = False

print(type(a)) # <class 'bool'>

So, whenever you see True or False, Python treats them as values of type bool.

2. Why Boolean is Important

Boolean values are the backbone of decision-making in programming. Every time you write conditions like:

  • If a user is logged in
  • If a number is greater than another
  • If a file exists

You are essentially working with Boolean values. For example:

age = 18
print(age >= 18) # True

3. Boolean Values from Expressions

In Python, Boolean values are often not written explicitly. Instead, they are produced as the result of expressions.

print(10 > 5) # True
print(10 < 5) # False
print(10 == 10) # True
print(10 != 10) # False

These comparisons always return either True or False.

4. Boolean in Conditional Statements

The real power of Boolean values becomes visible when used in control flow statements like if.

is_logged_in = True

if is_logged_in:
print("Welcome to the system")
else:
print("Please log in")

5. Boolean with Logical Operators

Python provides logical operators to combine Boolean values:

  • and → True if both conditions are True
  • or → True if at least one condition is True
  • not → Reverses the Boolean value

Examples

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

6. Boolean and Numbers

In Python, Boolean is actually a subclass of integer (int), which leads to some interesting behavior.

  • True behaves like 1
  • False behaves like 0

This feature can be useful in counting and logical computations, but it should be used carefully to avoid confusion.

7. Boolean Conversion (Truthiness)

Python allows you to convert other data types into Boolean using the bool() function.

print(bool(10)) # True
print(bool(0)) # False
print(bool("Hello"))# True
print(bool("")) # False

8. Boolean in Loops

Boolean values are often used to control loops.

running = True

while running:
print("Loop is running")
running = False

Here, the loop runs as long as the condition remains True.