Boolean logic & truth tables · ตรรกะ布尔 & ตารางความจริง
Boolean values
- A Boolean value is either
TrueorFalse. - A condition such as
age >= 18produces a Boolean. - Computers build everything from these two values.
ค่า Boolean
- ค่า Boolean คือ
TrueหรือFalseอย่างใดอย่างหนึ่ง - เงื่อนไขเช่น
age >= 18จะสร้างค่า Boolean - คอมพิวเตอร์สร้างทุกอย่างจากสองค่านี้
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
andเป็น True ก็ต่อเมื่อทั้งสองข้างเป็น Trueorเป็น True เมื่ออย่างน้อยด้านหนึ่งเป็น Truenotเปลี่ยน True เป็น False, และ False เป็น 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.
ตารางความจริง
- ตารางความจริง รายชื่อทุกอินพุตที่เป็นไปได้และเอาต์พุตของมัน
- อินพุตสองตัว (A, B) ให้สี่แถว: 00, 01, 10, 11
Truth table — try every input · ตารางความจริง — ลองทุกอินพุต
and is true only when both are true; or when at least one is. · and จะเป็นจริงก็ต่อเมื่อ ทั้งสอง เป็นจริง; or เมื่อ อย่างน้อยหนึ่ง เป็นจริง
Build a truth table with loops
- A nested loop over
False, Truewalks through all the combinations.
สร้างตารางความจริงด้วยลูป
- ลูปซ้อนเหนือ
False, Trueจะวนผ่านทุกชุดค่าผสม
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.
เกทเพิ่มเติม: NAND, NOR, XOR
- NAND คือ
not (a and b); NOR คือnot (a or b) - XOR เป็น True เมื่ออินพุต ต่างกัน:
a != b
In the exam: logic gates
- Know the gates NOT, AND, OR, NAND, NOR, XOR and their truth tables.
ในการสอบ: เกตรถ logic
- รู้จักเกท NOT, AND, OR, NAND, NOR, XOR และตารางความจริงของพวกมัน
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.
ข้อผิดพลาดที่พบบ่อย
ANDจะเป็นจริงก็ต่อเมื่ออินพุตทั้งสองเป็นจริงเท่านั้น; ส่วนORจะเป็นจริงเมื่ออินพุตอย่างน้อยหนึ่ง值是จริงNOTกลับค่า- ตารางความจริงรายการทุกชุดค่าผสมของอินพุต
Now you try
- Each function returns the gate's Boolean output.
- Press Check answer to test your code.
ลองดูเลย
- ฟังก์ชันแต่ละฟังก์ชันคืนค่าเอาต์พุต Boolean ของเกต
- กด Check answer เพื่อทดสอบโค้ดของคุณ
Write AND_gate(a, b) that returns True only when both inputs are True. · เขียน AND_gate(a, b) ที่จะคืนค่า True ก็ต่อเมื่ออินพุตทั้งสองเป็น True
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write XOR_gate(a, b) that returns True when the two inputs are different, and False when they are the same. · เขียน XOR_gate(a, b) ที่จะคืนค่า True เมื่ออินพุตทั้งสอง ต่างกัน และ False เมื่อเหมือนกัน
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write NAND_gate(a, b) that gives the opposite of AND: True unless both inputs are True. Use not (a and b). · เขียน NAND_gate(a, b) ที่ให้ค่า ตรงข้าม ของ AND: True เมื่อไม่ทั้งอินพุตเป็น True ใช้ not (a and b)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่