← Back

Activity 2.1 Logic Crash Course

Introduction

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 == 100True
100 == 99False
"abc" == "abc"True
"ABC" == "abc" False
True == TrueTrue
True == FalseFalse
Not Equal To (!=)Result
100 != 100False
100 != 99True
"abc" != "abc"False
"ABC" != "abc" True
True != TrueFalse
True != FalseTrue
Greater than/Less than (>,<)Result
100 > 100False
100 > 99True
0 < 100True
0 < -1False
Greater than or equal to/Less than or equal (>=,<=)Result
100 >= 100True
100 <= 99False
Not (not)Result
not TrueFalse
not FalseTrue
not (100 > 100)True

Skills to Practice

Instructions

Setup

  1. Create a new Python application. Name the project 2.1 Logic.
  2. Click Create.

Code

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}")

Debug

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

Tips, Tricks, and Reflection