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.
ערכים בוליאניים
- ערך בוליאני הוא או
TrueאוFalse. - תנאי כמו
age >= 18מייצר ערך בוליאני. - מחשבים בונים את הכל מ这两两个值。
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הוא נכון רק כאשר שני הצדדים נכונים.orהוא נכון כאשר לפחות אחד מהצדדים נכון.notהופך 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 היא נכונה כאשר הקלטים הם שונים:
a != b.
In the exam: logic gates
- Know the gates NOT, AND, OR, NAND, NOR, XOR and their truth tables.
במבחן: שערים לוגיים
- יש לדעת את השערים 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.
כעת תנסו בעצמכם
- כל פונקציה מחזירה את התוצאה הבוליאנית של השער.
- לחץ על בדוק תשובה כדי לבדוק את הקוד שלך.
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. · לחץ על הרץ כדי לראות את התוצא כאן.
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. · לחץ על הרץ כדי לראות את התוצא כאן.
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. · לחץ על הרץ כדי לראות את התוצא כאן.