Python For Loop Tutorial

What are Loops?

Loops are programming constructs that repeat a block of code multiple times until a certain condition is met. They help in automating repetitive tasks efficiently.

Loops ऐसे programming constructs हैं जो एक कोड ब्लॉक को कई बार तब तक दोहराते हैं जब तक कोई शर्त पूरी नहीं हो जाती। ये repetitive tasks को आसान और efficient बनाते हैं।

Why Use Loops?

Without loops, repetitive tasks would require writing the same code many times, which is inefficient and error-prone. Loops reduce code length, improve readability, and help handle large data easily.

Loops के बिना, repetitive tasks के लिए बार-बार एक जैसा कोड लिखना पड़ता जो असुविधाजनक और गलती के लिए ज़्यादा मौका देता है। Loops से कोड छोटा, पढ़ने में आसान और बड़े डेटा को संभालना आसान होता है।

How to Use For Loop in Python?

The for loop in Python iterates over a sequence (like list, tuple, string, or range) and executes the indented block for each item.

Python में for loop किसी sequence (जैसे list, tuple, string, या range) के ऊपर iterate करता है और हर item के लिए indented code block execute करता है।

for variable in sequence:
    # code to execute

10 Examples of Python For Loop

Example 1: Loop through a list
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    print(fruit)

Iterates over a list and prints each fruit.

एक list में iterate कर हर fruit print करता है।

Output:

apple
banana
cherry
Example 2: Loop through a string
for ch in 'hello':
    print(ch)

Loops through each character of a string and prints it.

string के हर अक्षर पर loop लगाकर उसे print करता है।

Output:

h
e
l
l
o
Example 3: Using range() to loop numbers
for i in range(5):
    print(i)

Loops through numbers from 0 to 4 using range().

range() से 0 से 4 तक numbers पर loop लगाता है।

Output:

0
1
2
3
4
Example 4: Loop with start and end in range()
for i in range(2, 6):
    print(i)

Loops numbers from 2 to 5 using range(start, end).

range(start, end) से 2 से 5 तक numbers पर loop।

Output:

2
3
4
5
Example 5: Loop with step in range()
for i in range(1, 10, 2):
    print(i)

Loops numbers from 1 to 9 with step 2 using range(start, end, step).

range(start, end, step) से 1 से 9 तक step 2 के साथ loop।

Output:

1
3
5
7
9
Example 6: Loop through a tuple
nums = (10, 20, 30)
for num in nums:
    print(num)

Loops through a tuple and prints each number.

tuple में iterate कर हर number print करता है।

Output:

10
20
30
Example 7: Nested for loop
for i in range(1, 3):
    for j in range(1, 3):
        print(i, j)

Nested loops: outer and inner loop printing pairs of i and j.

Nested loops: बाहरी और आंतरिक loops i और j के जोड़े print करते हैं।

Output:

1 1
1 2
2 1
2 2
Example 8: Loop with else block
for i in range(3):
    print(i)
else:
    print('Loop ended')

Uses else block executed after loop finishes normally.

else block जो loop के सही तरीके से खत्म होने पर execute होता है।

Output:

0
1
2
Loop ended
Example 9: Loop through dictionary keys
d = {'a': 1, 'b': 2}
for key in d:
    print(key)

Loops through dictionary keys.

dictionary की keys पर loop।

Output:

a
b
Example 10: Loop through dictionary items
d = {'a': 1, 'b': 2}
for key, value in d.items():
    print(key, value)

Loops through dictionary key-value pairs.

dictionary के key-value जोड़ों पर loop।

Output:

a 1
b 2