- Subhasish Mishra
Python 3 tutorial for beginners in Hindi-Chapter 16(Loop)
Updated: Nov 10, 2020
Loops in Python
Loop एक ऑपरेशन की repeatition प्रक्रिया है।Python में हमारे पास 2 प्रकार के loop होते हैं।
For Loop
While Loop
For Loop
For loop एक fixed range के लिए repetitive process है।
Syntax
for variable in sequence:
statement 1
statement 2
other statements
sequence कोई भी iterable हो सकता है जैसे list ,set ,tuple,dictionary ,string,file और range().
range()
range() एक function है जो 3 parameters ग्रहण करता है।
Start
End/Stop
Step
By default start का value zero(0) होता है।
Stop के value देना अनिवार्य है।
By default step का value one(1) होता है।अगर step positive हो तो < और अगर negative हो तो > इस्तमाल होता है।
Example
For Positive step
range(5)----->range(0,5,1)
start = 0
stop = 5
step = 1(positive)
0<5 (True)
0 + step (0+1=1)
1<5 (True)
1 + step (1+1=2)
2<5 (True)
2 + step (2+1=3)
3<5 (True)
3 + step (3+1=4)
4<5 (True)
4 + step (4+1=5)
5<5 (False)
For negative step
range(5,0,-1)
start = 5
stop = 0
step = -1(negative)
5>0 (True)
5 + step (5-1=4)
4>0 (True)
4 + step (4-1=3)
3>0 (True)
3 + step (3-1=2)
2>0 (True)
2 + step (2-1=1)
1>0 (True)
1 + step (1-1=0)
0>0 (False)
Write a program to print odd numbers from 1 to 10
for x in range(1,11,2):
print(x)
Output:
1
3
5
7
9
Python में print() function "/n"(नई line) के साथ ख़तम होती है। इसीलिए हर बार output नई लाइन में show करता है। इसको बदलने केलिए यानि सारे output एक ही line में print करने के लिए हम "end" इस्तमाल करते हैं। end को हम कोई भी value assign कर सकते हैं।
for x in range(5):
print(x,end="")
Output: 0 1 2 3 4
Write a program to display a multiplication table of n
for a in range(1,11,1):
print(n,"*",a,"=",(n*a))
String in Loop

s1 = "Pyhton"
for x in s1:
print(x)
Output:
P
y
t
h
o
n
Write a program to print a string in reverse order
s1 = "Pyhton"
for x in s1[::-1]:
print(x,end=" ")
Output:
n o t h y P
Write a program to print A to Z
for i in range(65,91):
print(char(i),end=" ")
Note: char() का उपयोग किसी विशेष ASCII value पर मौजूद character को print करने के लिए किया जाता है।
Nested for Loop(Pattern Programming)

1.Pattern 1
for x in range(3):
for y in range(3):
print(y+1,end=" ")
print()
Output:
1 2 3
1 2 3
1 2 3
2.Pattern 2
for x in range(1,6):
for y in range(x):
print(y=1,end=" ")
print()
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
3.Pattern 3
for x in range(1,6):
for z in range(5,1,-1):
print(" ",end=" ")
for y in range(x):
print(y+1,end=" ")
print()
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
4.Pattern 4
for x in range(5,0,-1):
for y in range(x):
print(y+1,end=" ")
Output:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
5.Pattern 5
for x in range (5,0,-1):
for y in range(5,x,-1):
print(" ",end=" ")
for z in range(x):
print(y+1,end=" ")
print()
Output:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
For with else
Python में हम for loop साथ else block भी define कर सकते हैं। यदि For loop पूरी तरह से execute होता है, तो ही else ब्लॉक के statements execute honge।
for var in sequence:
statement 1
statement 2
else:
statement 3
statement 4
other statements
Example
for x in range(1,11,2):
print(x,end=" ")
else:
print("Odd numbers from 1 to 10")
Output:
1 3 5 7 9
Odd numbers from 1 to 10
break: ब्रेक एक ऐसा कीवर्ड है जिसका उपयोग बीच में लूप को समाप्त करने के लिए किया जाता है।
continue: Continue कीवर्ड का उपयोग करके, 'continue ' के बाद के स्टेटमेंट को छोड़ दिया जाएगा और लूप को फिर से execute होगा ।
Example
for x in range(1,11,2):
if x==7:
break
print(x,end=" ")
Output:
1 3 5
for x in range(1,11,2):
if x==7:
continue
print(x,end=" ")
Output:
1 3 5 9
इस chapter में हमने for loop, उस से जुड़े keywords और patten program के बारे में पढ़ा। अगले चैप्टर में while loop के बारे में पढ़ेंगे।
अगर आपको chapterwise notes chhiye तो निचे कमेंट करे।