Lists (1-D arrays) · القوائم (مصفوفات 1-D)
A list holds many values
- A list keeps many values in one variable.
- Write the items inside square brackets:
[10, 20, 30]. - The items can be numbers, strings, or a mix.
القائمة تحتوي على قيم متعددة
- القائمة تحتفظ بالعديد من القيم في متغير واحد.
- اكتب العناصر داخل الأقواس المربعة:
[10, 20, 30]. - يمكن أن تكون العناصر أرقامًا، أو نصوصًا، أو مزيجًا منهما.
Get and change items
- Use an index, just like a string:
nums[0]is the first item. nums[-1]is the last item.- You can also change an item:
nums[0] = 99.
الحصول على عناصر وتغييرها
- استخدم الفهرس (index)، تمامًا كما في السلسلة النصية:
nums[0]هو العنصر الأول. nums[-1]هو العنصر الأخير.- يمكنك أيضًا تغيير عنصر:
nums[0] = 99.
fruits = ["apple", "pear", "plum"]
print(fruits[0])
print(fruits[-1])
Add items and count them
nums.append(x)addsxto the end of the list.len(nums)tells you how many items there are.- A list can grow as your program runs.
إضافة عناصر وحساب عددها
nums.append(x)يضيفxإلى نهاية القائمة.len(nums)يخبرك بعدد العناصر.- يمكن للقائمة أن تكبر أثناء تشغيل البرنامج.
fruits = ["apple", "pear"]
fruits.append("plum")
print(len(fruits))
print(fruits)
Loop over a list
for item in nums:visits each item in turn.- The loop variable holds one item per pass.
- This is the easy way to process every value.
التكرار عبر القائمة
for item in nums:يزور كل عنصر على التوالي.- يحمل متغير الحلقة عنصرًا واحدًا في كل دورة.
- هذه هي الطريقة سهلة لمعالجة كل قيمة.
scores = [10, 20, 30]
total = 0
for s in scores:
total = total + s
print(total)
In Cambridge pseudocode
- A list is the exam's 1-D array. Note: exam arrays often start at index
1, but Python lists start at0.
في الرمز الوهمي لكامبريدج
- القائمة هي مصفوفة ثنائية الأبعاد للامتحان 1. ملاحظة: غالبًا ما تبدأ مصفوفات الامتحان من الفهرس
1، لكن قوائم بايثون تبدأ من0.
DECLARE Scores : ARRAY[1:3] OF INTEGER
Scores[1] ← 10
Scores[2] ← 20
Scores[3] ← 30
FOR I ← 1 TO 3
OUTPUT Scores[I]
NEXT I
Common mistakes
- The first item is
a[0]and the last isa[-1];a[len(a)]is out of range. b = adoes not copy the list — both names share it. Usea.copy()to make a real copy.appendadds one item to the end.
أخطاء شائعة
- العنصر الأول هو
a[0]والأخير هوa[-1]؛a[len(a)]خارج النطاق. b = aلا ينسخ القائمة — كلا الاسمين يشاركان إليها. استخدمa.copy()لعمل نسخة حقيقية.appendيضيف عنصرًا واحدًا إلى النهاية.
Now you try
- Build or loop over a list in each task.
- Press Check answer to test your code.
الآن جرب بنفسك
- قم ببناء القائمة أو التكرار عليها في كل مهمة.
- اضغط على تحقق من الإجابة لاختبار الكود الخاص بك.
A list changes in place · القائمة تتغير في مكانها
A list is ordered and indexed; append/change/pop all modify it. · القائمة مرتبة ومُؤشَّرة؛ إضافة/تغيير/حذف كلهم يعدّلونها.
Create a list colors holding "red", "green", "blue". Then append "yellow" so the list has four items. · أنشئ قائمة colors تحتوي على "red"، "green"، "blue". ثم ⌈⌊ append ⟩ "yellow" لتكون القائمة بأربعة عناصر.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Loop over the list nums and add up its values. Store the total in total (the answer is 66). · مرر على القائمة nums واجمع قيمها. احفظ المجموع في total (الجواب هو 66).
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Find the largest value in nums with a loop. Start largest at the first item, then loop through nums and keep the bigger value. Store the answer in largest (it is 23). · اكتب دالة nums ترجع السلسلة النصية largest ثم الاسم ثم nums. لذا فإن largest ترسل 23.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Build a new list backwards with the items of nums in reverse order. Use a loop, and do not change nums itself. · بني قائمة جديدة backwards بعناصر nums بترتيب عكسي. استخدم حلقة، ولا تعدّل nums نفسه.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.