Computers aren’t limited to performing arithmetic calculations. We are able to program computers to make decisions using logical expressions. When a computer can make decisions, it’s able to determine different outcomes in a program. Here are a few example scenarios:
These examples all use instances of decision making. Think of it like a computer being able to answer yes or no, correct or incorrect, or more specifically, True or False. Programming languages have a data type called a boolean that represents the values True and False. The study of computing logical expressions is known as boolean logic. Logical expressions in programming result in either a True or False answer. Let’s revisit the examples above:
Just as math has arithmetic operators (+ - * /
), Python has two categories of boolean operators: comparison operators and logical operators. We won’t cover every logical operator in this lesson, but there is more to come.
Here is a brief rundown of the various operators: equality ==
, inequality !=
, greater than (>
), less than (<
), greater than or equal to (>=
), less than or equal to (<=
), and not (not
). Logic expressions can be constructed using different data types combined with these operators. The result will be either true or false. Here’s an example:
1000 == 1000
Is 1000 equal to 1000? It is, so the result is the value True. You can see a number of different examples in the tables below. These are known as truth tables.
Equal To (== ) | Result |
---|---|
100 == 100 | True |
100 == 99 | False |
"abc" == "abc" | True |
"ABC" == "abc" | False |
True == True | True |
True == False | False |
Not Equal To (!= ) | Result |
---|---|
100 != 100 | False |
100 != 99 | True |
"abc" != "abc" | False |
"ABC" != "abc" | True |
True != True | False |
True != False | True |
Greater than/Less than (> ,< ) | Result |
---|---|
100 > 100 | False |
100 > 99 | True |
0 < 100 | True |
0 < -1 | False |
Greater than or equal to/Less than or equal (>= ,<= ) | Result |
---|---|
100 >= 100 | True |
100 <= 99 | False |
Not (not ) | Result |
---|---|
not True | False |
not False | True |
not (100 > 100) | True |
==
) and inequality (!=
)>
) and less than (<
)>=
) and less than or equal (<=
)not
)name = "Mr. Mortimer"
age = 30
bank_account = 150
is_a_teacher = True
is_a_millionaire = False
print(f"Is my name Mr. Mortimer? {name == 'Mr. Mortimer'}")
print(f"Is my name Mr. Mertens? {name == 'Mr. Mertens'}")
print(f"My name is not Mr. Merriman: {name != 'Mr. Merriman'}")
print(f"Am I old enough to drive? {age >= 16}")
print(f"Do I have enough to buy a Dodge Challenger? {bank_account > 31000}")
print(f"{name} is a teacher: {is_a_teacher == True}")
print(f"{name} is a millionaire: {is_a_millionaire}")
print(f"So, {name} isn't a millionaire? {not is_a_millionaire}")
Is my name Mr. Mortimer? True
Is my name Mr. Mertens? False
My name is not Mr. Merriman: True
Am I old enough to drive? True
Do I have enough to buy a Dodge Challenger? False
Mr. Mortimer is a teacher: True
Mr. Mortimer is a millionaire: False
So, Mr. Mortimer isn't a millionaire? True