Skip to content

Booleans

Introduction

Boolean values, or bool, represent one of two values: True or False. They are commonly used in conditional statements and loops to control the flow of a program.

Creating Boolean Values

You can create a boolean value by assigning True or False to a variable:

Creating boolean values
is_connected = True
admin_is_up = False
has_power = True

A good idea when naming boolean variables is to use a prefix like is_, has_, or can_ to indicate that the variable is a boolean. This makes the code more readable and helps you understand the purpose of the variable. E.g, using the above boolean variables, we can write conditional statements like this:

Using boolean variables in conditional statements
if is_connected:
    print("The device is connected.")
else:
    print("The device is not connected.")

Boolean Operators

Python provides several operators to perform boolean operations:

  • and: Returns True if both operands are True
  • or: Returns True if at least one operand is True
  • not: Returns True if the operand is False
Boolean AND operator
is_connected = True
admin_is_up = False
has_power = True

operational_up = is_connected and admin_is_up and has_power
print(operational_up)  # Output: False

# let's change the value of admin_is_up
admin_is_up = True

operational_up = is_connected and admin_is_up and has_power
print(operational_up)  # Output: True

We will use a different more well-suited example to demonstrate the or operator.

Boolean OR operator
path1_is_up = False
path2_is_up = True

network_available = path1_is_up or path2_is_up
print(network_available)  # Output: True

The not operator is used to negate a boolean value:

Boolean NOT operator
is_connected = True

if not is_connected:
    print("The device is not connected.")
else:
    print("The device is connected.")

Truthy and Falsy Values

In Python, every value has an associated boolean value. Some values are considered True and others False. The following values are considered False:

  • False
  • None
  • 0 (zero)
  • 0.0 (zero as a float)
  • "" (empty string)
  • [] (empty list)
  • {} (empty dictionary)
  • () (empty tuple)
  • set() (empty set)

All other values are considered True. This is useful when you want to check if a value is empty or not:

Checking if a list is empty
my_list = []

if my_list:
    print("The firts item in the list is: ", my_list.pop(0))
else:
    print("The list is empty! No items to show!")

We can use this knowledge to create a default value for a user input, using the or operator and the input function as well as implicitly evaluating the input value as a boolean.

First consider this code:

Using the or operator to set a default value
user_input = input("Enter your name: ")
if not user_input:
    user_input = "Anonymous"

This code asks the user for their name and sets the default value to "Anonymous" if the user doesn't enter anything. We can simplify this code using the or operator:

Using the or operator to set a default value
user_input = input("Enter your name: ")
name = user_input or "Anonymous"
print(f"Hello {name}")

This code is equivalent to the previous one. If the user doesn't enter anything, the or operator will return the second value, "Anonymous", which will be assigned to user_input.

Even more concise, we can combine the input and assignment in a single line:

Using the or operator to set a default value
user_input = input("Enter your name: ") or "Anonymous"
print("Hello,", user_input)

If the user press enter without typing anything, the default value "Anonymous" will be used. This is because an empty string is considered False. Hence, the or operator will return the second value.

Summary

In this section, we covered the basics of boolean values in Python, including how to create, use in conditional statements, and perform boolean operations. Booleans are essential for controlling the flow of a program and making decisions based on conditions.