Python · Chapter 2.4 · Basic level
Loops (for — Automatic Robot Machine!)
लूप्स (for — बार-बार काम करने वाली ऑटोमैटिक मशीन!)
Imagine if your teacher said 'Write I Love Coding 100 times!' A normal person writes 100 lines, but a coder writes just 2 lines using a for loop! for i in range(5): tells Python's robot to repeat the indented block 5 times automatically (i = 0, 1, 2, 3, 4), or range(1, 6) counts from 1 to 5!
सोचो अगर कोई कहे कि '100 बार I Love Coding लिखो!' बिना कोडिंग के 100 लाइन लिखनी पड़ेंगी, पर कोडर सिर्फ़ 2 लाइन का for लूप चलाता है! for i in range(5): कंप्यूटर की रोबोट मशीन को 5 बार अपने-आप चलाता है, और range(1, 6) 1 से 5 तक पहाड़ा भी बना सकता है!
Words and symbols in this chapter
for- Keyword that starts a repeating loop.
- लूप (दोहराव वाली मशीन) शुरू करने का शब्द।
i- Loop counter variable — holds the current step number on each turn.
- काउंटर डिब्बा — हर चक्कर में अभी कौन सा नंबर चल रहा है, वह याद रखता है।
in range(5)- Generates 5 steps (
0, 1, 2, 3, 4).range(1, 6)generates1, 2, 3, 4, 5. - 5 बार गिनती चलाता है (
0से4)।range(1, 6)लिखने पर1से5तक गिनता है। : + 4 Spaces- Colon at end of
forline + 4 spaces before repeated lines! forलाइन के अंत में:और दोहराई जाने वाली लाइन के आगे 4 स्पेस!
Ready example
print("--- 5 Times Message ---")
for i in range(5):
print("I Love Coding")
print("--- Table of 2 ---")
for n in range(1, 6):
print("2 x", n, "=", 2 * n)
What you will see: Watch the animated Step-by-Step Loop Counter (Step 1 → Step 2 → ...) spin through every iteration!
प्रिव्यू में Step 1 → Step 2 → Step 3 → Step 4 → Step 5 लूप काउंटर एक-एक चक्कर दिखाएगा!
Line by line
for i in range(5):- Repeats the indented print line 5 times (
i = 0, 1, 2, 3, 4). - अंदर वाली लाइन को 5 बार दोहराता है।
print("I Love Coding")- Prints
'I Love Coding'on every turn of the loop. - हर चक्कर में
'I Love Coding'छापता है। for n in range(1, 6):- Counts
nfrom 1 up to 5 to print 2's multiplication table! 1से5तक गिनती चलाकर 2 का पहाड़ा छापता है!
Common mistake
✗ Wrong
for i in range(5)
print(i)✓ Right
for i in range(5):
print(i)range(5) gives 0, 1, 2, 3, 4 (stops before 5!), and needs : plus 4 spaces below.
range(5) गिनती 0 से 4 तक चलाता है (5 से पहले रुकता है), और लाइन के अंत में : ज़रूरी है!
Your task
Use for i in range(5): to print "I Love Coding" 5 times!
for i in range(5): लूप चलाकर 5 बार "I Love Coding" प्रिंट करवाओ!
- Line 1: Write
for i in range(5):(with colon:at the end!).
लाइन 1:for i in range(5):लिखो (आख़िर में:ज़रूर लगाओ)। - Line 2: Press Tab (4 spaces) and write
print("I Love Coding").
लाइन 2: 4 स्पेस आगे बढ़करprint("I Love Coding")लिखो। - Click ▶ Run & Preview!
▶ Run & Preview दबाओ!
Starter code for the task
print("Robot machine shuru...")
# TODO: for i in range(5): likho (colon : ke saath)
# aur agli line mein 4 spaces dekar print("I Love Coding")Do this task in the classroom: your code is checked automatically and you get hints if something is missing.
Recap
for i in range(5):repeats the indented block 5 times.range(5)gives 0, 1, 2, 3, 4;range(1, 6)gives 1 to 5.- Do not forget the colon
:and the 4-space indent.
for i in range(5):अंदर वाले block को 5 बार दोहराता है।range(5)देता है 0, 1, 2, 3, 4;range(1, 6)देता है 1 से 5।- Colon
:और 4 स्पेस का indent मत भूलना।
Quick quiz
What numbers does range(4) generate in a Python for loop?
Python में range(4) कौन-से नंबर जनरेट करता है?
Show the answer
Answer: B (0, 1, 2, 3)
Python starts counting from 0 and stops right before 4 -> 0, 1, 2, 3!
Python 0 से गिनती शुरू करता है और 4 से ठीक पहले रुकता है -> 0, 1, 2, 3!
Practice problems for this chapter
- Predict: How Many Overs?
बताओ: कितने ओवर छपेंगे?
Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.