The for
loop in Python is used to iterate over a sequence like list, tuple, string, or range. It repeats a block of code for each item.
Python में for
loop का उपयोग list, tuple, string या range जैसे sequence पर चलने के लिए होता है। यह हर item के लिए कोड को repeat करता है।
for i in range(1, 6):
print(i)
Output:
1 2 3 4 5Explanation: Loops through numbers from 1 to 5.
for i in range(2, 11, 2):
print(i)
Output:
2 4 6 8 10
for ch in "Python":
print(ch)
Output:
P y t h o n
colors = ["red", "green", "blue"]
for color in colors:
print(color)
Output:
red green blue
for i in range(1, 11):
print("5 x", i, "=", 5*i)
Output:
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
for i in range(1, 6):
print("Square of", i, "is", i*i)
Output:
Square of 1 is 1 Square of 2 is 4 Square of 3 is 9 Square of 4 is 16 Square of 5 is 25
text = "education"
vowels = "aeiou"
count = 0
for ch in text:
if ch in vowels:
count += 1
print("Vowel count:", count)
Output:
Vowel count: 5
s = "hello"
for ch in reversed(s):
print(ch)
Output:
o l l e h
items = ["pen", "book", "bottle"]
for index, value in enumerate(items):
print(index, ":", value)
Output:
0 : pen 1 : book 2 : bottle
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
Output:
1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3