Python Booleans
In Python, Booleans represent one of the simplest yet most powerful data types.
A Boolean value can be True or False, and it is heavily used in decision-making, conditions, loops, and logic building.
This topic moves from basics to hard & tricky concepts, exactly how it appears in real coding and interviews.
What Are Booleans in Python?
Booleans represent truth values.
python
x = True
y = False
print(x)
print(y)
python
print(type(True))
print(type(False))
Boolean Values from Comparisons
Booleans are often the result of comparison operators.
python
print(10 > 5)
python
print(10 == 5)
python
print(10 != 5)
Comparison Operators
| Operator | Meaning |
|---|---|
== | Equal |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
python
a = 10
b = 20
print(a < b)
print(a >= b)
print(a == b)
Boolean in if Statements
Booleans control program flow.
python
age = 18
if age >= 18:
print("Eligible to vote")
python
is_logged_in = False
if is_logged_in:
print("Welcome")
else:
print("Please login")
The bool() Function
The
bool() function converts values into True or False.python
print(bool(1))
python
print(bool(0))
python
print(bool("Python"))
python
print(bool(""))
Truthy and Falsy Values (VERY IMPORTANT)
Not everything is explicitly
True or False.
Some values are considered Falsy, everything else is Truthy.Falsy Values in Python
False00.0""[]{}()None
python
print(bool([]))
python
print(bool({}))
python
print(bool(None))
python
print(bool(" "))
Boolean with Logical Operators
AND (and)
Returns
True if both are true.python
print(True and True)
python
print(True and False)
python
print(10 > 5 and 5 > 2)
OR (or)
Returns
True if any one is true.python
print(True or False)
python
print(False or False)
python
print(5 > 10 or 10 > 5)
NOT (not)
Reverses the Boolean value.
python
print(not True)
python
print(not False)
python
print(not (10 > 5))
Boolean with Non-Boolean Values (TRICKY)
Logical operators return actual values, not always
True or False.python
print(10 and 20)
python
print(0 and 20)
python
print(10 or 20)
python
print("" or "Python")
Hidden Rule
and→ returns first falsy valueor→ returns first truthy value
Boolean Arithmetic (YES, THIS EXISTS)
Booleans are subclasses of integers.
python
print(True + True)
python
print(True + False)
python
print(False * 10)
python
print(True * 5)
Mind-Blowing Fact
True == 1 and False == 0
Boolean with is vs == (INTERVIEW LEVEL)
python
a = True
b = True
print(a == b)
print(a is b)
python
x = 1
y = True
print(x == y)
print(x is y)
Important
==→ checks valueis→ checks memory reference
Boolean from Membership Operators
python
print("a" in "apple")
python
print("z" in "apple")
python
print(3 in [1, 2, 3])
Boolean from Identity Checks
python
x = None
print(x is None)
python
y = []
print(y is [])
python
print(y == [])
Common Boolean Mistakes (VERY COMMON)
Mistake 1: Comparing with True
python
if x == True:
print("Yes")
Correct way:
python
if x:
print("Yes")
Mistake 2: Using is for numbers
python
print(1000 is 1000)
Unreliable behavior ❌
HARD Logical Questions (Practice)
Predict the Output
python
print(bool("False"))
python
print(True == 1)
python
print(False == 0)
python
print(True is 1)
python
print(not not True)
python
print(5 > 3 > 1)
python
print(5 > 3 and 3 > 10 or 2 > 1)
Summary
- Booleans store
TrueorFalse - Comparisons always return Booleans
- Truthy/Falsy values are crucial
and,or,nothave special behavior- Booleans behave like integers
isvs==is critical- Booleans are everywhere in Python logic
Exercise (Hard)
- Check if a list is empty using Boolean logic
- Write a condition that returns
Trueonly if a number is between 10 and 20 - Predict output of a chained comparison
- Explain why
bool("False")isTrue