- Home
- /
- Python Booleans
Python Tutorial
Introduction
Python Data Types & Oper...
Python Strings
Python Lists
Python Tuples
Python Sets
Python Dictionaries
Conditional Statements
Python Loops
Python Functions
Python Modules
Python OOPS
Advanced Topics
File Handling
- Home
- /
- Python Booleans
Python Booleans
Booleans in Python represent truth values—only two exist:
True
False
Though small in appearance, Booleans are powerful tools in decision-making logic.
Why Use Booleans?
Booleans help you evaluate conditions in your code. Take a look at this basic example:
x = 13
if x > 13:
print("X is a prime number.")
else:
print("X is not a prime number.")
Here, the expression x > 13
returns a Boolean value. Since 13 > 13
is false, the code inside the else
block runs.
The bool() Function
Python has a built-in function called bool()
which evaluates any value and converts it to True
or False
. Here’s how different data types behave:
Booleans with None
print("None as Boolean:", bool(None)) # False
Booleans with Numbers
print("Zero:", bool(0)) # False
print("Positive integer:", bool(14)) # True
print("Negative float:", bool(-2.5)) # True
print("Complex number:", bool(1 + 1j)) # True
Zero is always
False
.Any non-zero number (int, float, complex) is
True
.
Booleans with Strings
print("Non-empty string:", bool("Learn With Arshyan")) # True
print("String with digits:", bool("42")) # True
print("Empty string:", bool("")) # False
Any string that has content (even spaces or numbers) is
True
.An empty string is
False
.
Booleans with Lists
print("Empty list:", bool([])) # False
print("List with elements:", bool([0, 1])) # True
Booleans with Tuples
print("Empty tuple:", bool(())) # False
print("Tuple with data:", bool(("Python", 3.10))) # True
Booleans with Dictionaries and Sets
print("Empty dictionary:", bool({})) # False
print("Non-empty dictionary:", bool({"key": "value"})) # True
print("Empty set:", bool(set())) # False
print("Non-empty set:", bool({"apple", "banana"})) # True
Summary
Data Type | Example | Boolean Value |
---|---|---|
None | None | False |
Integer | 0 / 23 | False / True |
Float | 0.0 / 3.14 | False / True |
Complex | 0j / 5+2j | False / True |
String | "" / "Hello" | False / True |
List | [] / [1, 2] | False / True |
Tuple | () / (3,) | False / True |
Dictionary | {} / {"a": 1} | False / True |
Set | set() / {"x", "y"} | False / True |
Final Note
Booleans are everywhere in Python—from if
statements to loops and function checks. Mastering how values evaluate to True
or False
is key to writing logical, bug-free code.
Keep experimenting. Even something as small as bool("")
can teach you a lot about how Python sees your data.