Counting loops · حلقات العد
Repeat with for
- A
forloop repeats code a fixed number of times. - The body of the loop is indented, like an
if. - A loop variable holds a new value on each pass.
التكرار باستخدام for
- حلقة
forتكرر الأوامر عدداً ثابتاً من المرات. - جسم الحلقة مُزاح، مثل
if. - متغير الحلقة يحمل قيمة جديدة في كل دورة.
Counting with range
range(5)gives the numbers0, 1, 2, 3, 4(stops before 5).range(1, 6)gives1, 2, 3, 4, 5(start included, stop not).range(0, 10, 2)counts in steps of 2.
العد باستخدام range
range(5)يعطي الأرقام0, 1, 2, 3, 4(يتوقف قبل 5).range(1, 6)يعطي1, 2, 3, 4, 5(البداية مشمولة، والنهاية غير مشمولة).range(0, 10, 2)يعدّ بخطوات مقدارها 2.
for i in range(5):
print(i)
Build a total (accumulator)
- An accumulator is a variable that grows inside a loop.
- Start it at
0before the loop. - Add to it on each pass to build a running total.
بناء المجموع (المجمّع)
- الراكم هو متغير ينمو داخل الحلقة.
- ابدأه عند
0قبل الحلقة. - أضف إليه في كل دورة لبناء مجموع تراكمي.
total = 0
for n in range(1, 6):
total = total + n
print(total)
In Cambridge pseudocode
- A Python
for … rangeloop is the exam's count-controlledFOR … NEXT.
في الرمز الوهمي لكامبريدج
- حلقة Python
for … rangeهي عداد التحكم فيFOR … NEXTفي الامتحان.
Total ← 0
FOR I ← 1 TO 5
Total ← Total + I
NEXT I
OUTPUT Total
Words and a number on one line
- A comma inside
printputs a space between the parts. print("7 x", 1, "=", 7 * 1)prints7 x 1 = 7.+cannot join a number to text. Use the comma, or turn the number into text withstr.
كلمات ورقم في سطر واحد
- الفاصلة داخل
printتضع مسافة بين الأجزاء. print("7 x", 1, "=", 7 * 1)يطبع7 x 1 = 7.+لا يمكنه دمج رقم مع نص. استخدم الفاصلة، أو حوّل الرقم إلى نص باستخدامstr.
Common mistakes
range(n)counts0ton - 1, not1ton.range(a, b)stops beforeb.- The loop body must be indented under the
forline.
أخطاء شائعة
range(n)يعدّ من0إلىn - 1، وليس من1إلىn.range(a, b)يتوقف قبلb.- يجب إزاحة جسم الحلقة تحت سطر
for.
Now you try
- Use a
forloop withrange(...)in each task. - Press Check answer to test your code.
الآن جرب بنفسك
- استخدم حلقة
forمعrange(...)في كل مهمة. - اضغط على تحقق من الإجابة لاختبار الكود الخاص بك.
A counting loop, pass by pass · حلقة عد، خطوة بخطوة
A for loop repeats its body; watch the counter and the running total. · تكرّر حلقة for جسمها؛ راقب العداد والمجموع التراكمي.
Use a for loop to print the numbers 1 to 5, each on its own line. · استخدم حلقة for لطباعة الأرقام 1 إلى 5، كل منها في سطر منفصل.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Use a loop and an accumulator to add up every whole number from 1 to 100. Store the total in total. · استخدم حلقة ومجمّع لإضافة كل عدد صحيح من 1 إلى 100. خزن المجموع في total.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Multiply together every whole number from 1 to 5 (that is 1×2×3×4×5). Start product at 1 and store the result there. · اضرب كل عدد صحيح من 1 إلى 5 (أي 1×2×3×4×5). ابدأ بـ product عند 1 وخزن النتيجة هناك.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Print exactly one line: 7 x 1 = 7. Finish the print that is already started. A comma between the parts prints them with spaces: print("7 x", 1, "=", 7 * 1). · اطبع سطراً واحداً بالضبط: 7 x 1 = 7. أكمل print الذي بدأ بالفعل. الفاصلة بين الأجزاء تطبعها بمسافات: print("7 x", 1, "=", 7 * 1).
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Use a for loop to print the first five lines of the 7 times table: 7 x 1 = 7, 7 x 2 = 14, up to 7 x 5 = 35. The print from the last task is already inside the loop — change the 1 into the loop variable. · استخدم حلقة for لطباعة أول خمسة أسطر من جدول ضرب 7: 7 x 1 = 7، 7 x 2 = 14، حتى 7 x 5 = 35. المتغير print من المهمة الأخيرة موجود بالفعل داخل الحلقة — غيّر 1 ليكون متغير الحلقة.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.