Boolean logic & truth tables · LogicBoolean & Bảng chân lý
Boolean values
- A Boolean value is either
TrueorFalse. - A condition such as
age >= 18produces a Boolean. - Computers build everything from these two values.
Giá trị Boolean
- Một giá trị Boolean là
TruehoặcFalse. - Một điều kiện như
age >= 18tạo ra một giá trị Boolean. - Máy tính xây dựng mọi thứ từ hai giá trị này.
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.
AND, OR, NOT
andchỉ đúng khi cả hai vế đều đúng.orđúng khi ít nhất một vế là đúng.notđảo ngược True thành False, và False thành True.
print(True and False) # False
print(True or False) # True
print(not True) # False
A truth table
- A truth table lists every possible input and its output.
- Two inputs (A, B) give four rows: 00, 01, 10, 11.
Bảng chân lý
- Một bảng chân lý liệt kê mọi đầu vào có thể và đầu ra tương ứng.
- Hai đầu vào (A, B) tạo ra bốn hàng: 00, 01, 10, 11.
Truth table — try every input · Bảng chân lý — thử mọi đầu vào
and is true only when both are true; or when at least one is. · and chỉ đúng khi cả hai đều đúng; or đúng khi ít nhất một cái đúng.
Build a truth table with loops
- A nested loop over
False, Truewalks through all the combinations.
Xây dựng bảng chân lý với vòng lặp
- Vòng lặp lồng nhau trên
False, Trueduyệt qua tất cả các tổ hợp.
for a in [False, True]:
for b in [False, True]:
print(a, b, a and b)
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.
Các cửa cổng khác: NAND, NOR, XOR
- NAND là
not (a and b); NOR lànot (a or b). - XOR là True khi các đầu vào khác nhau:
a != b.
In the exam: logic gates
- Know the gates NOT, AND, OR, NAND, NOR, XOR and their truth tables.
Trong bài thi: cửa logic
- Biết các cửa NOT, AND, OR, NAND, NOR, XOR và bảng chân lý của chúng.
X ← A AND B
Y ← NOT (A OR B)
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.
Lỗi thường gặp
ANDchỉ đúng khi cả hai đầu vào đều đúng;ORđúng khi ít nhất một đầu vào là đúng.NOTđảo ngược giá trị.- Bảng chân lý liệt kê mọi tổ hợp của các đầu vào.
Now you try
- Each function returns the gate's Boolean output.
- Press Check answer to test your code.
Bây giờ bạn thử
- Mỗi hàm trả về đầu ra Boolean của cửa.
- Nhấn Check answer (Kiểm tra câu trả lời) để thử mã của bạn.
Write AND_gate(a, b) that returns True only when both inputs are True. · Viết AND_gate(a, b) chỉ trả về True khi cả hai đầu vào đều là True.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Write XOR_gate(a, b) that returns True when the two inputs are different, and False when they are the same. · Viết XOR_gate(a, b) trả về True khi hai đầu vào khác nhau, và False khi chúng giống nhau.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Write NAND_gate(a, b) that gives the opposite of AND: True unless both inputs are True. Use not (a and b). · Viết NAND_gate(a, b) cho kết quả ngược lại của AND: trả về True trừ khi cả hai đầu vào đều là True. Sử dụng not (a and b).
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.