Every decision your Python program makes comes down to one question: is this True or False? That question is answered by Booleans, one of the simplest and most important data types in the language. This tutorial covers exactly what a Boolean is, how Python represents truth values, and how to use them to control the flow of your code.
A Boolean is a data type that can hold only one of two values: True or False. The name comes from George Boole, a 19th-century mathematician who developed the branch of algebra that deals with logical operations on true/false values. In Python, the Boolean type is called bool, and it is one of the built-in types available without importing anything.
Booleans are everywhere in Python. When you write an if statement, run a while loop, or compare two numbers, a Boolean value is produced behind the scenes. Understanding how they work gives you control over every branching decision in your programs.
True, False, and the bool Type
Python spells its two Boolean constants with an uppercase first letter: True and False. These are reserved keywords, meaning you cannot use them as variable names. Attempting to write true or false in lowercase will raise a NameError because Python treats them as undefined variable names, not Boolean values.
is_active = True
is_deleted = False
print(type(is_active)) # <class 'bool'>
print(type(is_deleted)) # <class 'bool'>
The type() function confirms that both variables belong to the bool class. There are only two instances of bool in all of Python: True and False. No third value exists.
bool Is a Subclass of int
One detail that surprises many beginners is that bool is a subclass of int. This was established when the bool type was added to Python in version 2.3 through PEP 285. Because of this inheritance, True is numerically equal to 1 and False is numerically equal to 0.
print(isinstance(True, int)) # True
print(True == 1) # True
print(False == 0) # True
print(True + True) # 2
print(False + 1) # 1
Although you can use True and False in arithmetic, doing so in general application code hurts readability. The one practical exception is using sum() to count how many items in a collection satisfy a condition, since sum() adds each True as 1 and each False as 0.
# Counting passing scores with sum()
scores = [85, 42, 91, 68, 73, 55, 90]
passing = sum(score >= 70 for score in scores)
print(passing) # 4
Build a statement that creates a Boolean variable named is_valid set to True:
Comparison Operators and Logical Operators
Booleans are rarely typed out as literals in real programs. Far more often, they are produced by comparison operators and combined with logical operators. These are the two main tools for generating Boolean values on the fly.
Comparison Operators
Comparison operators take two values and return True or False depending on whether the relationship holds. Python provides six of them: == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to).
temperature = 72
print(temperature > 100) # False
print(temperature == 72) # True
print(temperature != 50) # True
print(temperature <= 80) # True
Do not confuse = (assignment) with == (comparison). Writing if x = 5 is a syntax error in Python. The correct form is if x == 5.
Logical Operators: and, or, not
Logical operators combine or invert Boolean values. Python provides three: and, or, and not. The and operator returns True only when both sides are truthy. The or operator returns True when at least one side is truthy. The not operator flips a Boolean to its opposite value.
has_ticket = True
is_adult = True
# and — both must be True
can_enter = has_ticket and is_adult
print(can_enter) # True
# or — at least one must be True
has_pass = False
can_enter_vip = has_ticket or has_pass
print(can_enter_vip) # True
# not — inverts the value
is_banned = False
print(not is_banned) # True
- Returns True when
- Both operands are truthy
- Example
True and TruereturnsTrue;True and FalsereturnsFalse
- Returns True when
- At least one operand is truthy
- Example
False or TruereturnsTrue;False or FalsereturnsFalse
- Returns True when
- The single operand is falsy
- Example
not FalsereturnsTrue;not TruereturnsFalse
Python's and and or operators use short-circuit evaluation. In an and expression, if the left operand is falsy, Python returns it immediately without evaluating the right side. In an or expression, if the left operand is truthy, Python returns it without evaluating the right side. This means and/or do not always return a strict True or False -- they return one of their operands.
This code checks whether a user can access a restricted page. One line has a bug. Find it.
is_logged_in = True to is_logged_in == True (use the comparison operator, not assignment). Even better, write if is_logged_in and is_admin: since the variable is already a Boolean and does not need to be compared to True at all.
Truthy Values, Falsy Values, and the bool() Function
Python does not require that an if statement or while loop receive a literal True or False. Any Python object can be evaluated in a Boolean context, and Python will interpret it as either truthy (treated as True) or falsy (treated as False).
The built-in bool() function lets you see how Python classifies any value. Pass an object to bool(), and it returns True or False based on the standard truth-value testing rules.
What Is Falsy?
The following values evaluate to False in Python: the constant False itself, the constant None, numeric zeros of any type (0, 0.0, 0j), empty strings (''), empty lists ([]), empty tuples (()), empty dictionaries ({}), empty sets (set()), and empty ranges (range(0)). Custom objects can also be falsy if they define a __bool__() method that returns False or a __len__() method that returns 0.
# All of these are falsy
print(bool(False)) # False
print(bool(None)) # False
print(bool(0)) # False
print(bool(0.0)) # False
print(bool("")) # False
print(bool([])) # False
print(bool({})) # False
What Is Truthy?
Everything that is not falsy is truthy. That includes non-zero numbers, non-empty strings, non-empty collections, and objects that do not explicitly define themselves as falsy. One subtle point to keep in mind: a list that contains falsy values, such as [0, 0, 0], is still truthy because the list itself is not empty.
# All of these are truthy
print(bool(42)) # True
print(bool(-1)) # True
print(bool("hello")) # True
print(bool([1, 2, 3])) # True
print(bool([0, 0, 0])) # True (the list is not empty)
print(bool({"a": 1})) # True
Instead of writing if len(my_list) > 0:, experienced Python programmers write if my_list:. Python's truthiness rules make this equivalent and more readable. An empty list is falsy, and any non-empty list is truthy.
How to Use Booleans in Python
This step-by-step walkthrough covers the core skills for working with Boolean values in any Python program.
-
Create Boolean variables with True and False
Assign the keywords
TrueorFalsedirectly to a variable. Both keywords require an uppercase first letter. Usetype()to confirm the variable is of typebool. -
Generate Booleans with comparison operators
Use comparison operators like
==,!=,<,>,<=, and>=to compare two values. Each comparison returns eitherTrueorFalse, which you can store in a variable or use directly in a conditional. -
Combine conditions with logical operators
Use the
and,or, andnotoperators to combine or invert Boolean values. Theandoperator returnsTrueonly when both sides are truthy. Theoroperator returnsTruewhen at least one side is truthy. Thenotoperator flips a Boolean to its opposite. -
Apply Booleans in if statements and loops
Place a Boolean expression after the
iforwhilekeyword to control which block of code runs. Python evaluates the expression and executes the indented block only when the result is truthy.
"The bool type would be a straightforward subtype of the int type." — PEP 285, Python Enhancement Proposals
Python Learning Summary Points
- A Boolean in Python is the
booltype with exactly two values:TrueandFalse. Both must be capitalized. The type was introduced in Python 2.3 through PEP 285 and is a subclass ofint, makingTrueequal to1andFalseequal to0. - Comparison operators (
==,!=,<,>,<=,>=) return Boolean values, and logical operators (and,or,not) combine them. Together, these form the foundation of all conditional logic in Python programs. - Every Python object has a truth value. Falsy values include
False,None, zero, and empty collections. Everything else is truthy. Thebool()function converts any object to its Boolean equivalent, and Python applies these same rules automatically inifstatements andwhileloops.
With Booleans in your toolkit, you have the foundation for every if/else branch, every while loop, and every conditional expression you will write in Python. Practice with the exercises below to solidify what you have learned.
Frequently Asked Questions
A Boolean in Python is a built-in data type called bool that represents one of two possible values: True or False. Booleans are used to evaluate conditions, control program flow with if statements and loops, and represent the result of comparisons.
True and False are reserved keywords in Python and must be written with an uppercase first letter. Writing true or false (lowercase) will cause a NameError because Python does not recognize them as valid identifiers for Boolean values.
Yes. In Python, bool is a subclass of int. True is equivalent to the integer 1 and False is equivalent to the integer 0. This means you can use Boolean values in arithmetic expressions, though doing so is generally not recommended for readability.
True is the Boolean constant with a value of 1. Truthy refers to any Python object that evaluates to True when passed to the bool() function or used in a Boolean context like an if statement. For example, the integer 42, a non-empty string, and a non-empty list are all truthy even though they are not the value True itself.
The falsy values in Python are: False, None, numeric zeros (0, 0.0, 0j), empty strings (''), empty lists ([]), empty tuples (()), empty dictionaries ({}), empty sets (set()), and empty ranges (range(0)). Custom objects can also be falsy if they define a __bool__() method that returns False or a __len__() method that returns 0.
The bool() function converts any Python object into its Boolean equivalent, returning either True or False. It follows Python's standard truth-value testing rules: falsy values like 0, None, and empty collections return False, while everything else returns True.
Comparison operators like == (equal), != (not equal), < (less than), > (greater than), <= (less than or equal), and >= (greater than or equal) evaluate two values and return either True or False. For example, 5 > 3 returns True, and 10 == 20 returns False.
Python has three logical operators: and (returns True only if both operands are truthy), or (returns True if at least one operand is truthy), and not (inverts the Boolean value). These operators use short-circuit evaluation, meaning Python stops evaluating as soon as the result is determined.
Yes. Because bool is a subclass of int, True behaves like 1 and False behaves like 0 in arithmetic. For example, True + True returns 2, and sum([True, False, True]) returns 2. This behavior is useful for counting how many items in a collection meet a condition.
The bool type was added to Python in version 2.3, introduced through PEP 285. Before that, Python programmers used the integers 0 and 1 to represent false and true, and there was no standard Boolean type built into the language.