Notifications
Clear all
Topic starter 01/08/2025 11:15 pm
Python Boolean statements are all about making decisions—your code asks “Is this true?” and acts accordingly. Let’s break it down 🧠🔍
✅ What Is a Boolean?
A Boolean is a data type that can only be:
True
False
These are special keywords in Python and always start with a capital letter.
🔍 Using Booleans in Conditions
Booleans are most commonly used in if
statements:
is_raining = True
if is_raining:
print("Grab an umbrella!")
else:
print("Enjoy the sunshine!")
- If
is_raining
isTrue
, the first block runs. - If it’s
False
, theelse
block runs.
🧪 Boolean Expressions
You can create Boolean values using comparison operators:
x = 10
print(x > 5) # True
print(x == 10) # True
print(x < 3) # False
These expressions return True
or False
.
🔗 Logical Operators
Combine multiple Boolean expressions:
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("Welcome to the concert!")
and
: Both conditions must be true.or
: At least one must be true.not
: Reverses the Boolean value.
🧠 Evaluating Values with bool()
You can convert other types to Boolean using bool()
:
print(bool(0)) # False
print(bool(123)) # True
print(bool("")) # False
print(bool("hello")) # True
- Empty values (like
0
,""
,[]
,None
) areFalse
. - Non-empty values are
True
.