Programming paradigms · نماذج البرمجة
What is a paradigm?
- A programming paradigm is a style of organising a program.
- The same problem can be solved in different styles.
- Python lets you mix several paradigms in one program.
ما هو النموذج البرمجي؟
- النموذج البرمجي هو أسلوب لتنظيم البرنامج.
- يمكن حل نفس المشكلة بأساليب مختلفة.
- بايثون يسمح لك بدمج عدة نماذج برمجية في برنامج واحد.
Imperative and procedural
- Imperative code is a list of steps that change the program's state.
- It says how to do something, step by step (loops, assignments).
- Procedural style groups those steps into reusable procedures and functions.
الأمرية والإجراءية
- الكود الأمري هو قائمة خطوات تغير حالة البرنامج.
- يُخبرك كيف تفعل شيئًا، خطوة بخطوة (الحلقات، الإسنادات).
- النمط الإجرائي يجمع تلك الخطوات في إجراءات ودوال قابلة لإعادة الاستخدام.
def total(nums):
result = 0
for n in nums:
result = result + n
return result
print(total([2, 4, 6]))
Object-oriented
- Object-oriented code bundles data and the methods that act on it into objects.
- You model the problem as a set of classes that talk to each other.
- It suits large programs because each class is a self-contained part.
كائنية التوجه
- كود كائنية التوجه يجمع البيانات والدوال التي تعمل عليها في كائنات.
- تحاكي المشكلة كمجموعة من الفئات تتواصل مع بعضها البعض.
- يناسب البرامج الكبيرة لأن كل فئة جزء مستقل.
class Counter:
def __init__(self):
self.count = 0
def add(self):
self.count = self.count + 1
c = Counter()
c.add()
c.add()
print(c.count)
Declarative
- Declarative code says what you want, not how to compute it.
- The language or library works out the steps for you.
- Examples: a database query (SQL), or a Python list comprehension.
التصريحية
- الكود التصريحيي يقول ماذا تريد، وليس كيف تحسبه.
- اللغة أو المكتبة تحدد الخطوات نيابة عنك.
- أمثلة: استعلام قاعدة بيانات (SQL)، أو فهم قائمة بايثون.
nums = [1, 2, 3, 4, 5]
print(sum(nums)) # say WHAT: the total
print([n * n for n in nums]) # a list comprehension
Low-level
- Low-level code is close to the machine: assembly and machine code.
- The CPU runs it directly, so it is fast but hard to write.
- High-level languages like Python are translated down to it before running.
منخفضة المستوى
- الكود منخفض المستوى قريب من الآلة: لغة التجميع وكود الآلة.
- المعالج تنفيذه مباشرة، لذا فهو سريع لكن صعب الكتابة.
- لغات عالية المستوى مثل بايثون يتم ترجمتها إليه قبل التنفيذ.
Common mistakes
- Procedural is steps; object-oriented is objects with data and methods; declarative describes the result.
- Match the paradigm to the problem.
أخطاء شائعة
- الإجرائية هي خطوات؛ كائنية التوجه هي كائنات بها بيانات ودوال؛ التصريحية تصف النتيجة.
- طابق النموذج البرمجي مع المشكلة.
Now you try
- Each task solves a small problem in a different paradigm.
- Press Check answer to test your code.
الآن جرب بنفسك
- كل مهمة تحل مشكلة صغيرة بنموذج برمجي مختلف.
- اضغط على تحقق من الإجابة لاختبار الكود الخاص بك.
Procedural style. Write sum_list(nums) that uses a loop and an accumulator to add up the list and return the total. · النمط الإجرائي. اكتب sum_list(nums) تستخدم حلقة وتراكمية لجمع القائمة وإرجاع المجموع الكلي.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Declarative style. Write evens(nums) that returns a list of the even numbers, using a single list comprehension [... for ... if ...]. · النمط التصريحي. اكتب evens(nums) ترجع قائمة بالأعداد الزوجية، باستخدام تعبير قائمة واحد [... for ... if ...].
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Object-oriented style. Write a class Bag whose __init__(self, items) stores a list, and whose method total(self) returns the sum of those items. · نمط كائنات البرمجة. اكتب فئة Bag يكون فيها __init__(self, items) يخزن قائمة، وطريقة total(self) ترجع مجموع تلك العناصر.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.