….
Python Booleans – True or False Made Simple
In Python, Booleans are a special data type that represent truth values. They can only have two possible outcomes:
- True
- False
Booleans are widely used in programming to make decisions, control the flow of code, and validate conditions.
Boolean Values in Python
When you compare two values or evaluate an expression, Python returns either True
or False
:
print(10 > 5) # True
print(10 == 5) # False
print(10 < 5) # False
Booleans are especially useful inside conditional statements like if
, else
, and while
loops.
Using Booleans in Conditions
a = 20
b = 7
if a > b:
print("a is greater than b")
else:
print("a is not greater than b")
✅ Output: a is greater than b
Evaluating Values with bool()
Python has a built-in bool()
function that lets you check if a value is truthy or falsy.
print(bool("Hello")) # True
print(bool(15)) # True
print(bool("")) # False
print(bool(0)) # False
What Counts as True in Python?
By default, non-empty and non-zero values are treated as True.
Examples of values that return True
:
bool("GoNimbus") # True (non-empty string)
bool(123) # True (non-zero number)
bool(["Python", "AI"]) # True (non-empty list)
What Counts as False in Python?
Only a few values are considered False:
False
None
0
(integer or float)""
(empty string)[]
,()
,{}
(empty collections)- Objects with a
__len__
method that returns0
print(bool(False)) # False
print(bool(None)) # False
print(bool(0)) # False
print(bool("")) # False
print(bool([])) # False
Booleans in Custom Classes
You can define when your own objects should evaluate as True
or False
using the __len__
or __bool__
method.
class MyClass:
def __len__(self):
return 0 # behaves like an empty object
obj = MyClass()
print(bool(obj)) # False
Functions Returning Booleans
Functions often return a Boolean result to indicate success/failure or yes/no decisions.
def is_even(num):
return num % 2 == 0
print(is_even(10)) # True
print(is_even(7)) # False
Built-in Functions That Return Booleans
Python provides many built-in functions that return Booleans. One useful example is isinstance()
, which checks if a variable belongs to a specific data type.
x = 200
print(isinstance(x, int)) # True
print(isinstance(x, str)) # False
Real-World Use Cases of Booleans
- User Authentication
is_logged_in = True
if is_logged_in:
print("Welcome back, user!")
else:
print("Please log in.")
- Input Validation
email = "test@example.com"
if "@" in email:
print("Valid Email")
else:
print("Invalid Email")
- Loops and Control Flow
while True:
print("This will run forever unless stopped!")
break
Best Practices with Booleans
✔️ Use descriptive variable names like is_active
, has_data
, or can_login
.
✔️ Avoid writing conditions like if value == True:
→ instead, write if value:
.
✔️ Use Booleans to simplify logic in functions and conditions.
Quick Recap
- Booleans represent True or False.
- Used heavily in conditions, loops, and validations.
bool()
function evaluates values to truthy/falsy.- Most values are
True
except empty, zero, orNone
. - Functions and built-in methods often return Booleans.
👉 At GoNimbus, we break down Python fundamentals like Booleans into simple, practical lessons so you can apply them in real-world coding scenarios — from data validation to AI decision-making.