Logical operators في بايثون: تحكم ذكي في قراراتك البرمجية

اليوم سنتعلم أداة قوية ستجعل برامجك أكثر ذكاءً وقدرة على اتخاذ القرارات المعقدة: Logical operators.

في البرمجة، كثيراً ما نحتاج إلى التحقق من أكثر من شرط في وقت واحد. تخيل أنك تريد التحقق من أن المستخدم بالغ و لديه رصيد كافٍ لإتمام عملية شراء. هنا تأتي أهمية Logical operators لدمج هذه الشروط بسهولة.

ما هي Logical operators؟

Logical operators هي رموز خاصة تستخدم لربط تعبيرين منطقيين أو أكثر (يُعبر عنهما بـ True أو False) وإنتاج نتيجة منطقية جديدة.

هناك ثلاثة Logical operators أساسية في بايثون:

  • and (و)
  • or (أو)
  • not (ليس)

هذه الـ Operators تعمل مع القيم المنطقية (Boolean) التي تعرفنا عليها سابقاً (True و False).

الـ Operator AND (و)

الـ Operator and يعيد قيمة True فقط إذا كان كلا التعبيرين على جانبه صحيحين. إذا كان أحدهما أو كلاهما خاطئاً، فإن النتيجة تكون False.

جدول الحقيقة لـ AND:

التعبير الأول التعبير الثاني النتيجة
True True True
True False False
False True False
False False False

مثال عملي:

# Check if age is over 18 and balance is over 100
age = 20
balance = 150

# Use AND to check both conditions together
can_purchase = (age > 18) and (balance > 100)
print(can_purchase)  # Will print: True

# Another example
is_weekend = False
has_invitation = True

can_attend_party = is_weekend and has_invitation
print(can_attend_party)  # Will print: False (because it is not the weekend)

الـ Operator OR (أو)

الـ Operator or يعيد قيمة True إذا كان أحد التعبيرين على الأقل صحيحاً. يعيد False فقط إذا كان كلا التعبيرين خاطئين.

جدول الحقيقة لـ OR:

التعبير الأول التعبير الثاني النتيجة
True True True
True False True
False True True
False False False

مثال عملي:

# Check if there is a discount for employees or new customers
is_employee = True
is_new_customer = False

has_discount = is_employee or is_new_customer
print(has_discount)  # Will print: True (because the user is an employee)

# Another example: free entry for children or seniors
age = 65
is_child = age < 12
is_senior = age >= 65

free_entry = is_child or is_senior
print(free_entry)  # Will print: True

الـ Operator NOT (ليس)

الـ Operator not هو Operator أحادي (يعمل على تعبير واحد فقط) ويعكس القيمة المنطقية. إذا كان التعبير صحيحاً يصبح خاطئاً، والعكس صحيح.

جدول الحقيقة لـ NOT:

التعبير النتيجة
True False
False True

مثال عملي:

# Toggle user status
is_logged_in = False

# Check if the user is not logged in
is_logged_out = not is_logged_in
print(is_logged_out)  # Will print: True

# Another example: Check if the product is unavailable
is_available = True
is_out_of_stock = not is_available
print(is_out_of_stock)  # Will print: False

دمج Logical operators مع جمل if

يمكننا استخدام Logical operators مع جمل الشرط if لإنشاء شروط أكثر تعقيداً وذكاءً.

مثال متكامل:

# Club entry system
age = 25
has_membership = True
is_guest = False

# Entry allowed if age is 18+ and (has membership or is a guest)
if age >= 18 and (has_membership or is_guest):
    print("Welcome! You may enter.")  # This sentence will be executed
else:
    print("Sorry, you cannot enter.")

# Another example: check if age is under the legal limit
min_age = 18
user_age = 16

if not (user_age >= min_age):
    print("You are under the legal age for this content.")

التقييم القصير (Short-Circuit)

and وor لا يعيدان دائماً True أو False؛ يعيدان أحد the two Parameters. 0 or "default" تعيد "default"، و"user" or "guest" تعيد "user". هذا يجعل name = user_input or "مجهول" اختصاراً شائعاً، لكنه يفشل إذا كانت القيمة الصالحة 0 أو "". استخدم if user_input is None عندما تريد التمييز بين “غير موجود” و”فارغ”.

and يتوقف عند أول قيمة falsy، وor عند أول قيمة truthy. لذلك is_valid() and save() لا ينفذ save إذا فشل الشرط، وهذا مفيد لتجنب أخطاء متتالية.

أولوية الـ Operators

not أعلى أولوية من and، وand أعلى من or. التعبير not a and b يعني (not a) and b، لا not (a and b). أضف الأقواس عندما يختلط الثلاثة؛ فهي لا تغير النتيجة عند الصواب لكنها تمنع القراءة الخاطئة.

نتيجة غير منطقية؟

True and 0 تعيد 0، وليس False، لأن بايثون تعيد الـ Operator لا القيمة المنطقية. إذا احتجت bool صريحاً فغلف التعبير بـ bool(...). هذا الفرق يظهر عند إرجاع القيمة من دالة ثم استخدامها في JSON.

ملخص الدرس

  • and: يعيد True فقط إذا كان كلا الشرطين صحيحين
  • or: يعيد True إذا كان أحد الشرطين على الأقل صحيحاً
  • not: يعكس القيمة المنطقية (يغير True إلى False والعكس)
  • يمكن دمج هذه الـ Operators لإنشاء شروط معقدة وذكية
  • تستخدم بكثرة مع جمل الشرط if لاتخاذ قرارات برمجية متقدمة