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. · اضغط تشغيل لرؤية المخرجات هنا.