So far, we've seen how Python handles numbers (like 42
and 3.14
) and text (like "Hello, Python"
). But programs often need to represent simpler concepts: is something true or false? Is a condition met? Does a specific state exist? For this, Python provides the boolean data type.
The boolean type, named after George Boole who developed Boolean algebra, is fundamental in programming logic. It has only two possible values:
True
False
Notice the capitalization. True
and False
are reserved keywords in Python, representing these specific boolean values. You cannot use true
or false
(lowercase).
You can assign these values to variables just like numbers or strings:
is_learning = True
is_finished = False
print(is_learning)
print(is_finished)
Running this code would output:
True
False
These variables, is_learning
and is_finished
, now hold boolean values. They act like simple flags or switches within your program.
While you can assign True
or False
directly, boolean values often arise as the result of comparisons. When you use comparison operators (which we discussed earlier) like ==
(equal to), !=
(not equal to), >
(greater than), <
(less than), >=
(greater than or equal to), or <=
(less than or equal to), the outcome is always a boolean value.
Consider these examples:
age = 20
is_adult = age >= 18 # Is age greater than or equal to 18?
print(is_adult) # Output: True
temperature = 15.5
is_cold = temperature < 10.0 # Is temperature less than 10.0?
print(is_cold) # Output: False
name = "Alice"
is_bob = name == "Bob" # Is the name equal to "Bob"?
print(is_bob) # Output: False
In each case, the expression on the right side of the assignment operator (=
) is evaluated first. The comparison yields either True
or False
, and that result is then stored in the variable. This direct link between comparisons and boolean outcomes is a cornerstone of controlling program behavior.
Boolean values are the foundation for decision-making in Python. They are used extensively with logical operators (and
, or
, not
) to combine multiple conditions. For instance, you might want to check if a user is both logged in and has administrator privileges.
logged_in = True
is_admin = False
# 'and' requires both sides to be True for the result to be True
can_access_admin_panel = logged_in and is_admin
print(can_access_admin_panel) # Output: False
# 'or' requires at least one side to be True for the result to be True
can_view_content = logged_in or is_admin
print(can_view_content) # Output: True
# 'not' inverts the boolean value
is_guest = not logged_in
print(is_guest) # Output: False
We will look more closely at logical operators later in this chapter. They are essential tools for building more complex conditions, which you'll use frequently when directing the flow of your programs with if
statements (covered in the next chapter).
Python has a useful concept often called "truthiness". In contexts where a boolean value is expected (like in an if
statement, which you'll learn about soon), Python can evaluate values of other types as being either "truthy" (behaving like True
) or "falsy" (behaving like False
).
The following values are considered falsy in Python:
False
itself.None
(representing the absence of a value).0
, 0.0
).""
[]
()
{}
set()
Almost every other value in Python is considered truthy. This includes non-zero numbers, non-empty strings, lists with items, and so on.
Why is this helpful? It allows for concise checks. For example, instead of checking if a list my_list
has more than zero elements using len(my_list) > 0
, you can often just write if my_list:
, because a non-empty list is truthy, while an empty list is falsy.
items = []
if items: # items is empty, so it's falsy
print("There are items in the list.")
else:
print("The list is empty.") # This will be printed
user_name = "Charlie"
if user_name: # user_name is not empty, so it's truthy
print(f"Hello, {user_name}!") # This will be printed
else:
print("User name is missing.")
count = 0
if count: # count is 0, so it's falsy
print("Count is positive.")
else:
print("Count is zero.") # This will be printed
While True
and False
are the explicit boolean values, understanding truthiness helps interpret how Python makes decisions in conditional logic. This implicit boolean evaluation makes Python code often more readable and compact.
Booleans are simple but essential. They allow programs to represent states (True
/False
), evaluate conditions through comparisons and logical operators, and ultimately, make decisions, forming a core part of programming logic. You'll find yourself using them constantly as you build programs that react differently based on input or internal state.
© 2025 ApX Machine Learning