Computers aren’t just for arithmetic calculations; they can also make decisions based on certain conditions. This ability to make decisions allows programs to handle different outcomes. Let’s explore a few everyday scenarios where a computer might need to make decisions:
In each of these examples, the computer is evaluating a question that can be answered with “yes” or “no”, or more precisely, True or False . In programming, we use something called a boolean to represent these two values: true and false.
In Python, the bool
data type is used to store values that can be either True
or False
. This makes it perfect for logical expressions, which are questions or conditions that the computer can evaluate.
Example
is_raining = False
has_umbrella = True
In this example, is_raining
is set to False
because it’s not raining, while has_umbrella
is set to True
because you have an umbrella.
The study of how logical expressions are formed and evaluated is called boolean logic. In programming, these logical expressions are constructed using operators and values, and they always result in either True
or False
. Let’s revisit our earlier scenarios to see how they can be expressed as boolean logic:
True
or False
?True
or False
?True
or False
?Just as math has arithmetic operators (like +, -, *, /), Python has operators specifically for boolean logic. These operators allow you to create logical expressions that the computer can evaluate. There are several types of operators:
Comparison Operators
Equality == | Result |
---|---|
100 == 100 |
True |
100 == 99 |
False |
"abc" == "abc" |
True |
"ABC" == "abc" |
False |
True == True |
True |
True == False |
False |
Inequality != | Result |
---|---|
100 != 100 |
False |
100 != 99 |
True |
"abc" != "abc" |
False |
"ABC" != "abc" |
True |
True != True |
False |
True != False |
True |
Greater/Less than > <
|
Result |
---|---|
100 > 100 |
False |
100 > 99 |
True |
0 < 10 |
True |
0 < -1 |
False |
Greater/Less than or equal >= <=
|
Result |
---|---|
100 >= 100 |
True |
10 >= 99 |
True |
0 <= 0 |
True |
0 <= -1 |
False |
Logical Operators
There are two important operators in this category that are not listed: logical AND, and logical OR. We will cover these in subsequent lessons.
Logical Negation (NOT) not
|
Result |
---|---|
not True |
not False |
not False |
True |
not (100 > 100) |
True |
Let’s look at some examples of boolean expressions in Python:
age = 18
is_old_enough = age >= 18
In this example, the expression age >= 18
checks if the age is greater than or equal to 18. If the condition is true, is_old_enough
is set to True
; otherwise, it will be set to False
.
is_equal = (1000 == 1000)
Here, we are checking if 1000 is equal to 1000. Since this is obviously true, is_equal
is assigned the value True
.
Boolean logic is essential for enabling computers to make decisions. By using boolean values (True
or False
) and operators, we can write programs that evaluate conditions and respond accordingly. We’ll dive deeper into boolean logic as we continue, exploring how it can be combined with control flow statements like if statements to create more complex decision-making structures.
# Initialize variables
name = "Mr. Mortimer"
age = 30
bank_account = 150.00
is_a_teacher = True
is_a_millionaire = False
# Assign boolean expressions to variables
is_my_name_mortimer = name == "Mr. Mortimer"
is_old_enough_to_drive = age >= 16
can_buy_dodge_challenger = bank_account > 31000
is_senior_citizen = age >= 65
print("-Name Checks-")
print(f"Is my name Mr. Mortimer? {is_my_name_mortimer}")
print(f"Is my name Mr. Mertens? {name == 'Mr. Mertens'}")
print(f"My name is not Mr. Merriman: {name != 'Mr. Merriman'}")
print("\n-Age-Related Checks-")
print(f"Am I old enough to drive? {is_old_enough_to_drive}")
is_old_enough_to_rent_car = age >= 25
print(f"Am I old enough to rent a car? {is_old_enough_to_rent_car}")
print(f"Am I eligible for a senior citizen discount? {is_senior_citizen}")
print("-Bank Account Checks-")
print(f"Do I have enough to buy a Dodge Challenger? {can_buy_dodge_challenger}")
has_positive_balance = bank_account > 0
print(f"Do I have a positive bank balance? {has_positive_balance}")
print("\n-Boolean Checks-")
print(f"{name} is a teacher: {is_a_teacher}")
print(f"{name} is a millionaire: {is_a_millionaire}")
is_not_millionaire = not is_a_millionaire
print(f"So, {name} isn't a millionaire? {is_not_millionaire}")
# Bonus: logical AND and logical OR (Covered in 2.4)
print("\n-Logical Operator Examples-")
can_drive_and_buy_car = is_old_enough_to_drive and can_buy_dodge_challenger
print(f"Am I old enough to drive AND have enough to buy a Dodge Challenger? {can_drive_and_buy_car}")
can_drive_or_buy_car = is_old_enough_to_drive or can_buy_dodge_challenger
print(f"Am I old enough to drive OR have enough to buy a Dodge Challenger? {can_drive_or_buy_car}")
-Name Checks-
Is my name Mr. Mortimer? True
Is my name Mr. Mertens? False
My name is not Mr. Merriman: True
-Age-Related Checks-
Am I old enough to drive? True
Am I old enough to rent a car? True
Am I eligible for a senior citizen discount? False
-Bank Account Checks-
Do I have enough to buy a Dodge Challenger? False
Do I have a positive bank balance? True
-Boolean Checks-
Mr. Mortimer is a teacher: True
Mr. Mortimer is a millionaire: False
So, Mr. Mortimer isn't a millionaire? True
-Logical Operator Examples-
Am I old enough to drive AND have enough to buy a Dodge Challenger? False
Am I old enough to drive OR have enough to buy a Dodge Challenger? True