Copyright: Mateus de Oliveira Oliveira - September 2023 - For personal use only
Boolean Operators
Boolean operators, such as and , or and not are used to define Boolean expressions
in terms of simpler Boolean expressions.
The and Operator
Let A and B be Boolean expressions. The expression A and B evaluates to True
whenever both conditions A and B evaluates to True . The and operator is used to
verify if several conditions are satisfied at the same time.
Checking eligibility based on multiple conditions
In the following example we implement a program that states that the user is allowed to
drive if the user is at least 18 years old and has a driving license.
age = int(input("Age? "))
has_license = input("License? (Yes/No) ")
if age >= 18 and has_license=="Yes":
print("You are allowed to drive.")
else:
print("You are not allowed to drive.")
Age? 18
License? (Yes/No) Yes
You are allowed to drive.
Age? 18
License? (Yes/No) No
You are not allowed to drive.
Age? 17
License? (Yes/No) Yes
You are not allowed to drive.
Age? 17
License? (Yes/No) No
You are not allowed to drive.
Boolean Operators 1
Copyright: Mateus de Oliveira Oliveira - September 2023 - For personal use only
Checking if a number is inside a valid range
The following code prints a message whenever a number is inside a specified valid
range.
number = int(input("Number? "))
if number > 0 and number < 100:
print("Number inside valid range.")
Number? 50
Number inside valid range.
The or Operator
Let A and B be Boolean expressions. The expression A or B evaluates to True if
either condition A is True , condition B is True , or if both A and B are True . The or
operator is used to check whether at least one of the conditions is satisfied.
Checking eligibility based on at least one condition
In the following example we implement a program that states that the user is eligible to
a discount if the user is a student or a senior citizen (or both).
is_student = input("Are you a student? (Yes/No) ")
is_senior_citizen = input("Are you a senior citizen? (Yes/No) ")
if is_student == "Yes" or is_senior_citizen == "Yes":
print("You are eligible for a discount.")
else:
print("You are not eligible for a discount.")
Are you a student? (Yes/No) Yes
Are you a senior citizen? (Yes/No) Yes
You are eligible for a discount.
---------------------------------------
Are you a student? (Yes/No) Yes
Are you a senior citizen? (Yes/No) No
You are eligible for a discount.
Boolean Operators 2
Copyright: Mateus de Oliveira Oliveira - September 2023 - For personal use only
---------------------------------------
Are you a student? (Yes/No) No
Are you a senior citizen? (Yes/No) Yes
You are eligible for a discount.
---------------------------------------
Are you a student? (Yes/No) No
Are you a senior citizen? (Yes/No) No
You are not eligible for a discount.
Checking if a number is outside a valid range
The following code prints a message whenever a number is outside a specified valid
range.
number = int(input("Number? "))
if number < 0 or number > 100:
print("Number outside valid range.")
Number? 101
Number outside valid range.
The not Operator
Let A be a Boolean expression. The expression not A evaluates to True if A is False (
and vice versa). The not operator is used to invert the value of a Boolean expression.
is_online = input("Is the user online? (Yes/No) ")
if not is_online == "Yes":
print("The user is offline.")
else:
print("The user is online.")
Is the user online? (Yes/No) Yes
The user is online.
Boolean Operators 3
Copyright: Mateus de Oliveira Oliveira - September 2023 - For personal use only
Is the user online? (Yes/No) No
The user is offline.
Combining Boolean Operators
Boolean operators can be nested using parenthesis. The following code analyzes the
conditions to host o beach volleyball game based on weather conditions and available
number of players. If the weather is nice and the number of players is at least 6 then the
program suggests the organization of a game. Here, nice weather means that it is either
sunny or very warm.
is_sunny = input("Is it sunny? (Yes/No) ")
temperature = float(input("Temperature in Celcius? ")) # in degrees Celsius
player_count = int(input("Number of Players? ")) # number of players
if (is_sunny == "Yes" or temperature >= 25) and player_count >= 6:
print("Organize a beach volleyball game!")
else:
print("Maybe choose another activity.")
Is it sunny? (Yes/No) Yes
Temperature in Celcius? 20
Number of Players? 6
Organize a beach volleyball game!
---------------------------------
Is it sunny? (Yes/No) Yes
Temperature in Celcius? 25
Number of Players? 5
Maybe choose another activity.
Boolean Operators 4