Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🐍 Unit 2 – Python Programming  ·  Lesson 07

for Loop, range() और Nested Loops

दोहराना क्यों?

Data का काम दोहराव से भरा है: 40 students के marks जोड़ना, 365 दिनों का temperature जाँचना, हर item की bill line print करना। Loop statements के block को दोहराता है, इसलिए वही code 4 items या 4 लाख items संभाल लेता है।

Starttotal = 0और items ?YesNoPrint totalअगला item लोtotal = total + itemStop
Loop का idea

for loop

for loop sequence (string, list, tuple, range) से एक-एक item लेकर हर एक के लिए body चलाता है।

Python
for city in ['Delhi', 'Mumbai', 'Chennai']:
    print('Weather report for', city)
for ch in 'DATA':
    print(ch, end='-')
print()
Weather report for Delhi Weather report for Mumbai Weather report for Chennai D-A-T-A-

range()

range(start, stop, step) start से stop − 1 तक numbers देता है, हर बार step बढ़ाते हुए। सिर्फ़ stop ज़रूरी है।

Python
print(list(range(5)))
print(list(range(3, 8)))
print(list(range(0, 50, 10)))
print(list(range(5, 0, -1)))
[0, 1, 2, 3, 4] [3, 4, 5, 6, 7] [0, 10, 20, 30, 40] [5, 4, 3, 2, 1]
range(0, 50, 10) →00101202303404
0 से शुरू, 10 से कूदो, 50 से पहले रुको

Totals, averages और counts

IP में loop के दो सबसे काम के patterns: accumulator (total += value) और counter (count += 1)।

Python
temps = [31.5, 33.0, 29.8, 35.2, 34.1, 30.6, 32.4]   # one week
total = 0
hot_days = 0
for t in temps:
    total += t
    if t > 33:
        hot_days += 1
print('Average temperature:', round(total / len(temps), 2))
print('Days above 33 °C:', hot_days)
Average temperature: 32.37 Days above 33 °C: 2
हफ़्ते का temperature28333843°CMonTueWedThuFriSatSun31.53329.835.234.130.632.4
दो दिन 33 °C से ऊपर

Input के साथ loops

Python
n = int(input('How many students? '))
total = 0
for i in range(1, n + 1):
    m = int(input('Marks of student ' + str(i) + ': '))
    total += m
print('Class average =', total / n)
How many students? 3 Marks of student 1: 78 Marks of student 2: 92 Marks of student 3: 64 Class average = 78.0

Nested loops

एक loop के अंदर दूसरा loop: outer loop के हर round पर inner loop पूरा चलता है।

Python
for i in range(1, 4):
    for j in range(1, 4):
        print(i, 'x', j, '=', i * j, end='   ')
    print()
1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9

Patterns और text charts

Python
for i in range(1, 6):
    print('#' * i)
for i in range(4, 0, -1):
    print(str(i) * i)
# ## ### #### ##### 4444 333 22 1

इसी idea से text में जल्दी से "bar chart" बनता है — Class XII में Matplotlib से बनने वाले charts की झलक:

Python
sales = {'Mon': 12, 'Tue': 7, 'Wed': 15, 'Thu': 4}
for day in sales:
    print(f'{day} {"█" * sales[day]} {sales[day]}')
Mon ████████████ 12 Tue ███████ 7 Wed ███████████████ 15 Thu ████ 4

while, break और continue

while loop तब तक दोहराता है जब तक condition True है। break loop से जल्दी बाहर निकालता है; continue अगले round पर ले जाता है।

Python
n = 5
i = 1
while i <= n:
    print(i * i, end=' ')
    i += 1
print()
for x in range(1, 20):
    if x % 7 == 0:
        print('First multiple of 7:', x)
        break
1 4 9 16 25 First multiple of 7: 7
Python
for marks in [45, -3, 88, 120, 67]:
    if marks < 0 or marks > 100:
        continue                  # skip invalid entries
    print('Valid marks:', marks)
Valid marks: 45 Valid marks: 88 Valid marks: 67

सारांश और practice

  • for item in sequence: हर item के लिए दोहराता है; range(start, stop, step) में stop शामिल नहीं।
  • Accumulator और counter patterns से totals, averages और counts।
  • Nested loops: हर outer round पर inner loop पूरा।
  • while condition पर दोहराता है; break बाहर; continue छोड़ता है।
✅ Practice

1) 1 से 10 तक के numbers के squares print करें। 2) 5 items की prices input करके total और सबसे महँगी price print करें। 3) यह pattern print करें: 1 / 1 2 / 1 2 3 / 1 2 3 4। 4) 1 से 100 तक कितने numbers 3 और 5 दोनों से divisible हैं?

Answer 4: 6 (15, 30, 45, 60, 75, 90)।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

range(3, 8) क्या देता है?
Numbers 3, 4, 5, 6 और 7। Stop value 8 शामिल नहीं है।
Nested loop की body कितनी बार चलती है?
Outer बार × inner बार। 3-3 बार के दो loops inner body 9 बार चलाते हैं।
continue का use क्या है?
यह current iteration का बाकी हिस्सा छोड़कर अगले पर जाता है, जैसे list process करते समय invalid data ignore करना।
← Class 11 Informatics Practices
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicPython में Lists आपकी प्रगति: 7 / 20 (35%) · Lesson 8 / 20 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें