Boolean logic & truth tables
หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
English
Boolean values
- A Boolean value is either
TrueorFalse. - A condition such as
age >= 18produces a Boolean. - Computers build everything from these two values.
English
AND, OR, NOT
andis True only when both sides are True.oris True when at least one side is True.notflips True to False, and False to True.

English
print(True and False) # False
print(True or False) # True
print(not True) # False
English
A truth table
- A truth table lists every possible input and its output.
- Two inputs (A, B) give four rows: 00, 01, 10, 11.
สำรวจ
Truth table — try every input
and is true only when ทั้งสองอย่าง are true; or when at least one is.
English
Build a truth table with loops
- A nested loop over
False, Truewalks through all the combinations.
English
for a in [False, True]:
for b in [False, True]:
print(a, b, a and b)
English
More gates: NAND, NOR, XOR
- NAND is
not (a and b); NOR isnot (a or b). - XOR is True when the inputs are different:
a != b.
English
In the exam: logic gates
- Know the gates NOT, AND, OR, NAND, NOR, XOR and their truth tables.
X ← A AND B
Y ← NOT (A OR B)
English
Common mistakes
ANDis true only when both inputs are true;ORis true when at least one is.NOTflips the value.- A truth table lists every combination of the inputs.
English
Now you try
- Each function returns the gate's Boolean output.
- Press Check answer to test your code.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Write AND_gate(a, b) that returns True only when ทั้งสองอย่าง inputs are True.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Write XOR_gate(a, b) that returns True when the two inputs are แตกต่างกัน, and False when they are the same.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Write NAND_gate(a, b) that gives the opposite of AND: True unless both inputs are True. Use not (a and b).
คลิก Run เพื่อดูผลลัพธ์ที่นี่