Putting it all together · دمج كل شيء معًا
Putting it all together
- You now know variables, input,
if, loops, lists, functions, and dictionaries. - A real program combines these small pieces into one useful tool.
- In this lesson you build a tiny class-scores analyzer, one function at a time.
دمج كل شيء معًا
- الآن تعرف المتغيرات، المدخلات،
if، الحلقات التكرارية، القوائم، الدوال، والقواميس. - البرنامج الحقيقي يجمع هذه القطع الصغيرة في أداة مفيدة واحدة.
- في هذا الدرس تبني محللاً صغيراً لـ درجات الصف، دالة واحدة في كل مرة.
The plan
- We have a list of test scores, like
[70, 90, 50, 80]. - We want three answers: the average, how many passed, and a short summary.
- We will write one small function for each job, then they work together.
الخطة
- لدينا قائمة من درجات الاختبارات، مثل
[70, 90, 50, 80]. - نريد ثلاث إجابات: المتوسط، عدد الـ ناجيين، وملخص قصير.
- سنكتب دالة صغيرة لكل مهمة، ثم تعمل معاً.
The analyzer calls each helper · المحلل يستدعي كل مساعد
A big program calls small functions in turn, each returning one answer. · برنامج كبير يستدعي دوال صغيرة بالتتابع، كل واحدة تُرجع إجابة واحدة.
Handy built-ins for lists
- Python gives you ready-made helpers that work on a list of numbers.
len(x)counts the items,sum(x)adds them,max(x)/min(x)find the largest / smallest.- Use them instead of writing a loop every time.
دوال مدمجة مفيدة للقوائم
- يوفر بايثون مساعدات جاهزة تعمل على قائمة من الأرقام.
len(x)يعدّ العناصر،sum(x)يجمعها،max(x)/min(x)يجدا الأكبر / الأصغر.- استخدمها بدلاً من كتابة حلقة تكرار في كل مرة.
scores = [70, 90, 50, 80]
print(len(scores)) # 4
print(sum(scores)) # 290
print(max(scores)) # 90
print(min(scores)) # 50
A pattern you will reuse: count items that pass a test
- A very common job: loop a list, and count the items that pass some test.
- Start a counter at
0, and add1each time theifis true. - Here we count the long words; soon you will count the passing scores.
نمط ستعيد استخدامه: عد العناصر التي تمر اختباراً معيناً
- مهمة شائعة جداً: حلقة تكرار على قائمة، و عدّ العناصر التي تمر باختبار ما.
- ابدأ العداد عند
0، وأضف1في كل مرة يكون فيهاifصحيحاً. - هنا نعد الكلمات الطويلة؛ قريباً ستعد الدرجات الناجية.
def how_many_long_words(words, min_length):
count = 0
for w in words:
if len(w) >= min_length:
count = count + 1
return count
print(how_many_long_words(["hi", "hello", "hey", "howdy"], 4)) # 2
Common mistakes
- Build in small pieces and test each one before adding the next.
- Read the whole task first, then plan before you code.
أخطاء شائعة
- بنِ البرنامج بقطع صغيرة واختبر كل جزء قبل إضافة التالي.
- اقرأ المهمة كاملة أولاً، ثم خطّط قبل البرمجة.
Now you try
- Build the analyzer in three steps:
average,count_passing, thenstats. - Each function is small. Together they turn a list of scores into real answers.
- Press Check answer after each one.
الآن جرب بنفسك
- بنِ المحلل على ثلاث مراحل:
average، ثمcount_passing، ثمstats. - كل دالة صغيرة. معاً تحول قائمة الدرجات إلى إجابات حقيقية.
- اضغط تحقق من الإجابة بعد كل دالة.
Write average(scores) that returns the average (mean) of a list of numbers: the total divided by how many. If the list is empty, return 0 (so it does not crash). Example: average([2, 4]) is 3.0. · اكتب average(scores) تُرجع المتوسط (الوسط الحسابي) لقائمة من الأرقام: المجموع مقسومًا على عددها. إذا كانت القائمة فارغة، أرجع 0 (لكي لا تنهار). مثال: average([2, 4]) هي 3.0.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write count_passing(scores, pass_mark) that returns how many scores are greater than or equal to pass_mark. Loop the list and count. Example: count_passing([40, 50, 60], 50) is 2. · اكتب count_passing(scores, pass_mark) تُرجع عدد الدرجات التي تكون أكبر من أو تساوي pass_mark. كرر القائمة واحسب. مثال: count_passing([40, 50, 60], 50) هي 2.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write stats(scores) (assume at least one score) that returns a dictionary with three keys: "highest", "lowest", and "average". Example: stats([70, 90, 50, 80]) returns {"highest": 90, "lowest": 50, "average": 72.5}. · اكتب stats(scores) (بافتراض وجود درجة واحدة على الأقل) تُرجع قائمة مفاتيح بثلاثة مفاتيح: "highest"، "lowest"، و"average". مثال: stats([70, 90, 50, 80]) تُرجع {"highest": 90, "lowest": 50, "average": 72.5}.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write grade_counts(scores) that returns a dictionary counting grades: A for 80 and above, B for 60 and above, C for the rest. Always include all three keys: grade_counts([85, 72, 90, 40]) is {"A": 2, "B": 1, "C": 1}. · اكتب grade_counts(scores) تُرجع قائمة مفاتيح تحسب الدرجات: A للدرجات 80 وما فوقها، B للدرجات 60 وما فوقها، C لباقي الدرجات. شمل دائماً جميع المفاتيح الثلاثة: grade_counts([85, 72, 90, 40]) هي {"A": 2, "B": 1, "C": 1}.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.