Kahibaro
Discord Login Register

Boolean values

Understanding Boolean Values

In Python, a Boolean (often shortened to bool) is a data type that can have only two possible values:

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:

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)  # True

You’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:

Booleans and Numbers

In Python, Booleans behave like numbers in some situations:

You can see this with int():

print(int(True))   # 1
print(int(False))  # 0

And in simple arithmetic:

print(True + True)   # 2
print(True + False)  # 1
print(False + False) # 0

This 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:

Example:

age = 15
is_adult = age >= 18
has_permission = False
print("Adult:", is_adult)          # Adult: False
print("Has permission:", has_permission)  # Has permission: False

This makes your code easier to read and understand.

Simple Practice Ideas

You can try writing small snippets in interactive mode:

  1. Create Boolean variables:
   likes_pizza = True
   is_student = False
  1. Use comparisons to create Booleans:
   temperature = 30
   is_hot = temperature > 25
  1. Use type() on them:
   print(type(is_hot))
  1. 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.

Views: 17

Comments

Please login to add a comment.

Don't have an account? Register now!