1 / 24
Programming · Grade 7 · Chapter 2

Logic Gates → Python Thinking

Bridging physical switches & Truth Tables into real Python boolean expressions.

Chapter Goals

What We Will Master Today

Here's a problem

From Switches to Code Sentences

Computers don't have brains — they have billions of microscopic transistors that act like light switches (ON = 1, OFF = 0). How do switches make decisions?

Discuss before revealing: How can simple ON/OFF switches calculate math or run video game rules?
Core Concept 1

The Binary World (Bits & Signals)

Every piece of computer logic relies on Boolean values: True (1) or False (0).

High Voltage (1 / True)

Switch CLOSED, current flows, light turned ON, button pressed.

Low Voltage (0 / False)

Switch OPEN, current stopped, light OFF, button released.

Physical Circuits

Series vs Parallel Circuits

Series Circuit (AND Logic)

Two switches in a row. Electricity flows ONLY if Switch A AND Switch B are both closed.

Parallel Circuit (OR Logic)

Two side-by-side tracks. Electricity flows if Switch A OR Switch B is closed.

Core Gate 1

The AND Gate

Output is 1 (True) ONLY if BOTH Input A and Input B are 1 (True).

Input AInput BOutput (A AND B)
0 (False)0 (False)0 (False)
0 (False)1 (True)0 (False)
1 (True)0 (False)0 (False)
1 (True)1 (True)1 (True)
Core Gate 2

The OR Gate

Output is 1 (True) if AT LEAST ONE input is 1 (True).

Input AInput BOutput (A OR B)
0 (False)0 (False)0 (False)
0 (False)1 (True)1 (True)
1 (True)0 (False)1 (True)
1 (True)1 (True)1 (True)
Core Gate 3

The NOT Gate (The Inverter)

Takes a single input and flips it to the opposite value.

Input AOutput (NOT A)
0 (False)1 (True)
1 (True)0 (False)
Real life example: Security alarm light is OFF when door is closed (1), turns ON when door breaks connection (0).
Smartboard Voting

Evaluate This Logic Output!

Input A = True (1)
Input B = False (0)
They pass into an OR gate, and then the result goes into a NOT gate.
Question: What is the final output? (Is it True or False?)
Advanced Gates

Combining Gates: NAND, NOR & XOR

NAND

AND gate followed by NOT. Opposite of AND.

NOR

OR gate followed by NOT. True only if both 0.

XOR

Exclusive OR. True if inputs are different.

Scratch to Python

Remember Scratch Green Boolean Blocks?

In Scratch, you snapped green hexagonal blocks inside `if` blocks:

< (score > 50) and (lives > 0) >
< (key_pressed) or (mouse_down) >
< not (touching color) >
Language Comparison

Visual Blocks vs Python Keywords

Scratch Visual Block

< <age > 12> and <has_ticket = true> >

Python Text Syntax

if age > 12 and has_ticket == True:

Core Concept 4

Python's Three Logical Operators

# 1. and (both must be True)
result1 = (5 > 2) and (10 < 20) # Evaluates to True

# 2. or (at least one is True)
result2 = (5 > 100) or (3 == 3) # Evaluates to True

# 3. not (flips True to False)
result3 = not (5 == 5) # Evaluates to False
Rulebook

Logical Operator Precedence

Just like math has PEMDAS, Python evaluates boolean logic in this strict order:

1
Parentheses ()
2
not operator
3
and operator
4
or operator
Spot the Mistake

Parentheses Change Everything!

expr1 = not True and False
# Step 1: not True -> False
# Step 2: False and False -> False

expr2 = not (True and False)
# Step 1: (True and False) -> False
# Step 2: not False -> True!
Real-World Code

Video Game Inventory Check

has_key = True
player_health = 75
is_poisoned = False

can_open_door = has_key and (player_health > 0) and not is_poisoned
print(can_open_door) # What gets printed?
Quick Check · Quiz 1

What is the output of an AND gate when Input A = 1 and Input B = 0?

A1 (True)
B0 (False)
C2
DUndefined
Click to reveal answer
Quick Check · Quiz 2

What does Python evaluate for: (10 > 5) or (2 > 100)?

ATrue
BFalse
CNone
DError
Click to reveal answer
Quick Check · Quiz 3

Which Scratch block corresponds to Python's not (x == y)?

A< (x = y) and (not y) >
B< (x = y) or (x = y) >
C< not < x = y > >
D< join x y >
Click to reveal answer
Quick Check · Quiz 4

Which logical operator flips a Boolean value to its opposite?

Aand
Bor
Cnot
Dnand
Click to reveal answer
Guided Practice

Translate English Rules to Python Expressions

Rule: "Allow roller coaster entry if height is over 120cm AND (age is 12 or older OR parent is present)."
allowed = (height > 120) and (age >= 12 or parent_present == True)
Common Trap

Misconception: if x == 1 or 2: in Python

In English we say "If x equals 1 or 2". But Python interprets or 2 as "is 2 truthy?".

✗ Wrong Python Syntax

if x == 1 or 2:

✓ Correct Python Syntax

if x == 1 or x == 2:

Summary & Takeaways

What We Learned in Chapter 2

Exit Ticket

Truth Table Challenge

Evaluate on your whiteboard:

a = True, b = False, c = True

What is: not a or (b and c)?

Next Chapter: Python Setup & Syntax!