Online Computer Courses Classes and Training Program

Python Loops (Part 7.1 – Section B)

Python Loops (Part 7.1 – Section B)


Nested for Loop, Multiple Tables Generator, Factorial Program, Sum of Numbers और Practical Logic

यह Section पिछले Part 7.1 (Section A) का निरंतर भाग है।


Nested for Loop क्या होता है?

अब तक आपने एक साधारण for Loop देखा।

लेकिन वास्तविक Programming में कई बार ऐसी स्थिति आती है जहाँ एक Loop के अंदर दूसरा Loop चलाना पड़ता है।

इसे Nested Loop कहा जाता है।

सरल शब्दों में—

जब एक Loop के अंदर दूसरा Loop लिखा जाए, उसे Nested Loop कहते हैं।


Real Life Example

मान लीजिए आप एक School में Computer Teacher हैं।

आपको पाँच Classes की Attendance Print करनी है।

प्रत्येक Class में 30 Students हैं।

यदि केवल एक Loop लगाएंगे—

for student in range(1,31):
    print(student)

तो केवल Students दिखाई देंगे।

लेकिन यदि आपको यह भी दिखाना है कि Student किस Class का है, तब एक Loop पर्याप्त नहीं होगा।

हमें दो Loops की आवश्यकता होगी।


Nested Loop का Concept

Class 1
   Student 1
   Student 2
   Student 3

Class 2
   Student 1
   Student 2
   Student 3

Class 3
   Student 1
   Student 2
   Student 3

ध्यान दीजिए—

बाहरी Loop Class बदल रहा है।

भीतरी Loop Students बदल रहा है।


पहला Nested Loop Program

for i in range(1,4):

    print("Class",i)

    for j in range(1,6):

        print("Student",j)

Output

Class 1
Student 1
Student 2
Student 3
Student 4
Student 5

Class 2
Student 1
Student 2
Student 3
Student 4
Student 5

Class 3
Student 1
Student 2
Student 3
Student 4
Student 5

यह Program कैसे काम करता है?

पहले Outer Loop चलता है।

Class = 1

अब Inner Loop पाँच बार चलता है।

फिर

Outer Loop

Class = 2

हो जाता है।

फिर Inner Loop फिर से पाँच बार चलता है।

यही प्रक्रिया तब तक चलती है जब तक Outer Loop समाप्त नहीं हो जाता।


Nested Loop को याद रखने की आसान Trick

कल्पना कीजिए—

आप एक Apartment में रहते हैं।

Apartment में

5 Floors हैं।

प्रत्येक Floor पर

10 Flats हैं।

Outer Loop

Floor

Inner Loop

Flat

यानी

Floor 1

Flat 1

Flat 2

Flat 3

....

Floor 2

Flat 1

Flat 2

यही Nested Loop है।


Multiple Tables Generator

अब तक आपने एक Number का Table बनाया था।

लेकिन यदि User को

1 से 10 तक सभी Tables चाहिए हों तो?

Nested Loop सबसे अच्छा समाधान है।

for table in range(1,11):

    print("\nTable of",table)

    for i in range(1,11):

        print(table,"x",i,"=",table*i)

अब Program अपने आप

1 का Table

2 का Table

3 का Table

...

10 तक के सभी Tables बना देगा।


यह वास्तविक जीवन में कहाँ उपयोग होता है?

मान लीजिए—

एक Coaching Institute में

50 Students हैं।

प्रत्येक Student के

10 Subjects हैं।

यदि Report Card बनानी हो—

तो Program कुछ इसी प्रकार चलता है।

Outer Loop

Student

Inner Loop

Subject


Example

students = ["Rahul","Aman","Ajay"]

subjects = ["Math","English","Science"]

for student in students:

    print("\nStudent :",student)

    for subject in subjects:

        print(subject)

Output

Student : Rahul

Math

English

Science

Student : Aman

Math

English

Science

Factorial क्या होता है?

Programming Interviews में सबसे अधिक पूछे जाने वाले Questions में से एक है—

Factorial Program

यदि

5!

लिखा जाए—

तो इसका अर्थ होता है—

5 × 4 × 3 × 2 × 1

उत्तर

120

Factorial Program

number = int(input("Enter Number : "))

factorial = 1

for i in range(1,number+1):

    factorial *= i

print("Factorial =",factorial)

Step by Step समझिए

यदि User ने लिखा—

5

तो

Loop चलेगा—

1

2

3

4

5

अब

1 × 2 × 3 × 4 × 5

=

120

Sum of Numbers Program

यदि User पूछे—

1 से 100 तक सभी Numbers का योग निकालिए।

क्या करेंगे?

Loop।

total = 0

for i in range(1,101):

    total += i

print(total)

Output

5050

यह Program कैसे काम करता है?

पहले

total = 0

अब

0+1

1+2

3+3

6+4

10+5

इसी प्रकार

100 तक जोड़ता रहता है।


User से Number लेकर Sum निकालना

number = int(input("Enter Number : "))

total = 0

for i in range(1,number+1):

    total += i

print("Total =",total)

यदि User

10

लिखता है—

उत्तर

55

Even Numbers Print करना

for i in range(2,51,2):

    print(i)

Odd Numbers Print करना

for i in range(1,50,2):

    print(i)

Squares Print करना

for i in range(1,11):

    print(i*i)

Cubes Print करना

for i in range(1,11):

    print(i**3)

Common Mistakes

1. Outer और Inner Loop को गड़बड़ कर देना।

2. range() की अंतिम सीमा भूल जाना।

याद रखें—

range(1,6)

का अर्थ

1

2

3

4

5

है।

6 शामिल नहीं होता।


3. Factorial में

factorial = 0

लिख देना।

सही

factorial = 1

Best Practices

  • Nested Loop तभी उपयोग करें जब वास्तव में आवश्यकता हो।
  • Variable Names अर्थपूर्ण रखें (student, subject, table)।
  • बार-बार उपयोग होने वाले Logic को Function में बदलें।
  • बड़े Loops में Comments लिखें ताकि Code पढ़ना आसान रहे।

इस Section में आपने क्या सीखा?

✅ Nested for Loop

✅ Multiple Tables Generator

✅ Student–Subject Example

✅ Factorial Program

✅ Sum of Numbers

✅ Even–Odd Series

✅ Square और Cube Programs

अब आपके पास Loops की मजबूत समझ विकसित हो चुकी है और आप कई वास्तविक समस्याओं को Loop की सहायता से हल कर सकते हैं।


Post a Comment

0 Comments