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:
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:
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: ReturnsTrueif both operands areTrueor: ReturnsTrueif at least one operand isTruenot: ReturnsTrueif the operand isFalse
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.
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:
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:
FalseNone0(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:
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:
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:
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:
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.