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))
True
False
<class 'bool'>

Boolean results from comparisons

Comparison operators return True or False:

OperatorMeaningExample
==Equal to8 == 8True
!=Not equal to8 != 5True
<, <=Less than or less than or equal to3 <= 5True
>, >=Greater than or greater than or equal to10 > 4True
age = 20
print(age >= 18)
print(age == 21)
print(age != 16)
True
False
True

Boolean operators

Python provides three logical operators:

OperatorMeaningResult
andLogical ANDTrue only when both conditions are true.
orLogical ORTrue when at least one condition is true.
notLogical NOTReverses a truth value.

The and operator

ABA and B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse
age = 25
has_id = True
print(age >= 18 and has_id)
True

The or operator

ABA or B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse
is_admin = False
is_owner = True
print(is_admin or is_owner)
True

The not operator

Anot A
TrueFalse
FalseTrue
has_error = False
print(not has_error)
print(not (6 > 1))
True
False

Truthiness and the bool() function

Python can test any object in a Boolean context. The following values are falsy:

  • False
  • None
  • Numeric zero values such as 0 and 0.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))
False
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
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
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
True
2
5

Although this behavior is valid, use explicit conversions when it improves readability.

Common mistakes

  • Write True and False with capital first letters.
  • Use == for value comparison and is mainly for identity checks such as is None.
  • Do not use if value == True when if value communicates 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