Python Variable & Data Type
Python Boolean
Learn Python Boolean with clear explanations, Python examples, and practical programming guidance.
What is a Boolean in Python?
A Boolean is a value that represents one of two logical states: True or False. Comparisons, conditions, and validation checks produce Boolean results.
Python's Boolean type is named bool. The values must start with capital letters. Lowercase true and false are ordinary names and will raise NameError unless they have been defined.
Creating Boolean values
is_logged_in = True
has_permission = False
print(is_logged_in)
print(has_permission)
print(type(is_logged_in))False
<class 'bool'>
Boolean results from comparisons
Comparison operators return True or False:
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | 8 == 8 → True |
!= | Not equal to | 8 != 5 → True |
<, <= | Less than or less than or equal to | 3 <= 5 → True |
>, >= | Greater than or greater than or equal to | 10 > 4 → True |
age = 20
print(age >= 18)
print(age == 21)
print(age != 16)False
True
Boolean operators
Python provides three logical operators:
| Operator | Meaning | Result |
|---|---|---|
and | Logical AND | True only when both conditions are true. |
or | Logical OR | True when at least one condition is true. |
not | Logical NOT | Reverses a truth value. |
The and operator
| A | B | A and B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
age = 25
has_id = True
print(age >= 18 and has_id)The or operator
| A | B | A or B |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
is_admin = False
is_owner = True
print(is_admin or is_owner)The not operator
| A | not A |
|---|---|
| True | False |
| False | True |
has_error = False
print(not has_error)
print(not (6 > 1))False
Truthiness and the bool() function
Python can test any object in a Boolean context. The following values are falsy:
FalseNone- Numeric zero values such as
0and0.0 - Empty strings and collections such as
"",[],(), and{}
Most other objects are truthy, including negative numbers and non-empty collections:
print(bool(0))
print(bool(1))
print(bool(""))
print(bool("Python"))
print(bool([]))
print(bool([1, 2]))
print(bool(None))True
False
True
False
True
False
Using Booleans in conditions
Use a Boolean expression directly in an if statement. You do not need to compare it with True explicitly:
has_items = True
if has_items:
print("The cart contains products.")
else:
print("The cart is empty.")For a negative check, use if not has_items:. This is clearer than writing if has_items == False:.
Short-circuit evaluation
Python evaluates and and or from left to right and may stop as soon as the result is known. This can prevent unsafe or unnecessary work:
name = "Asha"
if name and name[0] == "A":
print("The name starts with A.")The second condition is evaluated only when name is non-empty, so indexing it is safe in this example.
Identity and equality are different
Use == to compare values. Use is to check whether two names refer to the same object. In particular, use is None when checking for None:
first = [1, 2]
second = first
third = [1, 2]
print(first == third) # same values
print(first is second) # same object
print(first is third) # different objects
result = None
print(result is None)True
False
True
Membership operators
Use in and not in to test whether a value exists in a string, list, tuple, set, or dictionary:
languages = ["Python", "Java", "SQL"]
print("Python" in languages)
print("Go" not in languages)
print("Py" in "Python")True
True
Boolean values and integers
In Python, bool is a subclass of int. Therefore, True behaves like 1 and False behaves like 0 in numeric contexts:
print(True == 1)
print(False == 0)
print(True + True)
print(False + 5)True
2
5
Although this behavior is valid, use explicit conversions when it improves readability.
Common mistakes
- Write
TrueandFalsewith capital first letters. - Use
==for value comparison andismainly for identity checks such asis None. - Do not use
if value == Truewhenif valuecommunicates the intent clearly. - Remember that an empty string or collection is falsy, while a non-empty string such as
"False"is truthy. - Use parentheses when combining complex conditions so the intended precedence is clear.
Further Reading
Continue learning with these related Python tutorials:
Continue learning
