Table of Contents
Understanding Boolean Values
In Python, a Boolean (often shortened to bool) is a data type that can have only two possible values:
TrueFalse
These are used to represent truth values: something is either true or false.
Python uses Booleans heavily in decisions, conditions, and logic (you will use them a lot with if, elif, else, and loops later).
The Boolean Type: `bool`
The type of a Boolean value is bool.
print(type(True))
print(type(False))Output:
<class 'bool'>
<class 'bool'>
Like int and float, bool is a built-in type.
Writing `True` and `False` Correctly
Important rules:
TrueandFalsemust start with a capital letter.- They must be written exactly as
TrueandFalse(no quotes). - They are not strings.
Examples:
is_sunny = True
is_raining = False
print(is_sunny) # True
print(is_raining) # False
print(type(is_sunny)) # <class 'bool'>
If you write true, false, "True", or "False" incorrectly, Python treats them differently:
# This will cause an error:
# is_valid = true # NameError: name 'true' is not defined
# These are strings, not booleans:
yes_str = "True"
no_str = "False"
print(type(yes_str)) # <class 'str'>Booleans from Comparisons
A very common way to get Boolean values is from comparison expressions.
Any comparison returns either True or False.
a = 5
b = 10
print(a < b) # True
print(a > b) # False
print(a == b) # False
print(a != b) # TrueYou’ll learn all comparison operators in detail in the Conditions chapter; for now, just notice that they produce Boolean results.
Booleans in Simple Checks
You can store the result of a comparison in a Boolean variable:
age = 20
is_adult = age >= 18
print(is_adult) # True
print(type(is_adult)) # <class 'bool'>This pattern is very common:
- Have a question: “Is the user an adult?”
- Use a comparison:
age >= 18 - Store the answer:
is_adult = age >= 18 - Use the Boolean later:
if is_adult: ...(you’ll do this in the Conditions chapter)
Booleans and Numbers
In Python, Booleans behave like numbers in some situations:
Trueacts like1Falseacts like0
You can see this with int():
print(int(True)) # 1
print(int(False)) # 0And in simple arithmetic:
print(True + True) # 2
print(True + False) # 1
print(False + False) # 0This is useful later for things like counting how many conditions are true, but as a beginner, treat Booleans mainly as “yes/no” or “on/off” values, not as numbers.
Converting to Boolean with `bool()`
You can convert other values to Boolean using the bool() function. This gives the truthiness of a value.
Some key examples:
print(bool(1)) # True
print(bool(0)) # False
print(bool("hello")) # True (non-empty string)
print(bool("")) # False (empty string)
print(bool([1, 2, 3])) # True (non-empty list)
print(bool([])) # False (empty list)
You will see this used more when working with conditions and collections, but it’s helpful to know that bool(x) answers “Is x considered true?”
Naming Boolean Variables
Because Booleans express “yes/no”, it’s helpful to give them names that sound like questions or states:
Good Boolean variable names:
is_logged_inhas_paidis_adminis_emptyis_validis_activehas_errors
Example:
age = 15
is_adult = age >= 18
has_permission = False
print("Adult:", is_adult) # Adult: False
print("Has permission:", has_permission) # Has permission: FalseThis makes your code easier to read and understand.
Simple Practice Ideas
You can try writing small snippets in interactive mode:
- Create Boolean variables:
likes_pizza = True
is_student = False- Use comparisons to create Booleans:
temperature = 30
is_hot = temperature > 25- Use
type()on them:
print(type(is_hot))- Convert some values using
bool():
print(bool(0))
print(bool(100))
print(bool(""))
print(bool("Python"))
In later chapters, you will use these Boolean values directly in if statements, loops, and more complex logic.