Loop Statement in Python

Loop Statement in Python

while loop is used to execute number of statements till the condition passed in while is true. Once the condition is false, the control will come out of the loop.

व्हाइल लूप का उपयोग तब तक स्टेटमेंट की संख्या एक्सिक्यूट करने के लिए किया जाता है जब तक कि कंडीशन टू न हो। एक बार स्थिति फाल्स होने के बाद, कंट्रोल लूप से बाहर आ जाएगा।

We generally use this loop when we don’t know earlier, the number of times to iterate.

हम आम तौर पर इस लूप का उपयोग तब करते हैं जब हम पहले से पुनरावृत्ति की संख्या नहीं जानते हैं।

Syntax: सिंटैक्स

while test-expression:

statement(s)

Flow of while loop: व्हाइल लूप का फ्लो.

If the condition returns true, the set

of statements inside loop are executed and then the control jumps to the beginning of the loop for next iteration.

यदि स्थिति सही होती है, तो लूप के अंदर स्टेटमेंट के सेट को एक्सिक्यूट किया जाता है और फिर कंट्रोल अगले पुनरावृत्ति के लिए लूप की शुरुआत में जंप करता है।

If the condition returns false, the loop is terminated and the control jumps to the next statement in the program after the loop.

यदि कंडीशन फाल्स हो जाती है, तो लूप समाप्त हो जाता है और कंट्रोल लूप के बाद प्रोग्राम में अगले स्टेटमेंट पर पहुंच जाता है।

Example: Print 1 to 5 natural numbers. – 1 से 5 प्राकृतिक संख्याएँ प्रिंट करें।

count = 0 
print ("Enter while loop") 
while count <= 5:
 print (count)
 count = count + 1 
print ("Exit while loop")

Output:
Enter while loop
0
1
2
3
4
5
Exit while loop

for loop
फॉर लूप

If we want to execute some action for every element present in some sequence (that is a list or a tuple or a dictionary or a set or a string) then we should go for ‘for’ loop. It’s traditionally used when you have a piece of code which you want to repeat ‘n’ number of time. The ‘for’ loop is often distinguished by an explicit loop counter or loop variable.

यदि हम कुछ सिक्वेंस में मौजूद प्रत्येक एलीमेंट के लिए कुछ कोड एक्सिक्यूट करना चाहते हैं (यह एक सूची या एक टपल या एक डिक्शनरी या एक सेट या एक स्ट्रिंग है) फिर हमें ‘फॉर’ लूप के लिए जाना चाहिए। पारंपरिक रूप से इसका उपयोग तब किया जाता है जब आपके पास कोड का एक टुकड़ा होता है जिसे आप ‘n’ नंबर ऑफ तक रिपीट करना चाहते हैं। ‘फॉर लूप’ को अक्सर एक स्पष्ट लूप काउंटर या लूप वैरियेबल द्वारा अलग किया जाता है।

Syntax:

for iterator_var in sequence:

body statements (s)

Where sequence can be string or any collection (that is a list or a tuple or a dictionary or a set or a string). Body statement(s) will be executed for every element present in the sequence.

जहाँ सिक्वेंस स्ट्रिंग या कोई भी संग्रह हो सकता है (यह एक सूची या एक टपल या एक डिक्शनरी या एक सेट या एक स्ट्रिंग है)। सिक्वेंस में मौजूद प्रत्येक एलीमेंट के लिए बॉडी स्टेटमेंट एक्सिक्यूट किया जाएगा।

Example: To print characters present in the given string.

दिए गए स्ट्रिंग में मौजूद कैरेक्टर को प्रिंट करने के लिए।

str = "Python" 
for x in str:
print (x)

Output:
P
y
t
h
o
n

range ():

In Python, range () function accepts an integer and returns a range object, which is nothing but a sequence of integers. Let’s understand how to use range () function with the help of simple examples.

पॉयथन में, range() फंक्शन एक इंटीजर को स्वीकार करता है और एक रेंज ऑब्जेक्ट रिटर्न करता है, जो इंटीजर के सिक्वेन्स के अलावा कुछ नहीं है। आइए सरल उदाहरणों की सहायता से range() फंक्शन का उपयोग करने का तरीका जानें।

Syntax:

range (start, stop [, step])

range () takes three arguments. Out of the three 2 arguments are optional. i.e., Start and Step are the optional arguments. The most common use of range () function in python is to iterate sequence type (List, string etc) with for and while loop.

range () में तीन आगुर्मेन्ट होतें हैं। तीन में से दो आगुर्मेन्ट वैकल्पिक हैं। जैसे, स्टार्ट और स्टेप वैकल्पिक आगुर्मेन्ट हैं। पॉयथन में range() फंक्शन का सबसे आम उपयोग फॉर लूप या व्हाईल लूप के साथ सिक्वेन्स टाइप (लिस्ट, स्ट्रिंग आदि) को इटरेट करना है।

A start argument is a starting number of the sequence. i.e., lower limit. By default, it starts with 0 if not specified.

एक स्टार्ट आगुर्मेन्ट सिक्वेन्स का एक प्रारंभिक नम्बर है। यानी, लोवर लिमिट । डिफॉल्ट रूप से, यदि स्पेशिफाईड नहीं है, तो यह 0 से शुरू होता है।

A stop argument is an upper limit. i.e., generate numbers up to this number, The range() function doesn’t include this number in the result. (by default argument)

एक स्टॉप आगुर्मेन्ट एक अपर लिमिट है। यानी, इस संख्या तक की संख्याएँ जनरेट करें, range(0 फंक्शन परिणाम में इस संख्या को शामिल नहीं करता है। (डिफॉल्ट आगुर्मेन्ट द्वारा)। .

The step is a difference between each number in the result. The default value of the step is 1 if not specified.

Example: Using only one argument in range ()
range()  में केवल एक आगुर्मेन्ट का उपयोग करना।

print ("Using one argument in range () ") 
for i in range (6):
print (i, end=', ') 

Output:
Using one argument in range ()
0, 1, 2, 3, 4, 5,

Nested for loop
नेस्टेड फॉर लूप

Sometimes we can take a loop inside another loop, which are also known as nested for loop.

कभी-कभी हम एक लूप को दूसरे लूप के अंदर ले जा सकते हैं, जिसे नेस्टेड फॉर लूप भी कहा जाता है।

Example 1:

for i in range (2):
for j in range (2): 
print ("i = ", i, " j = ", j)

Output:
i =  0  j =  0
i =  0  j =  1
i =  1  j =  0
i =  1  j =  1

Example 2: Write a program to display *’s below form.

फॉर्म के नीचे * प्रदर्शित करने के लिए एक प्रोग्राम लिखें।

*
***
*****
*******

n = int (input ("Enter no.of rows : "))
for i in range (1, n+1) : 
     for j in range (1,i+1) :
        print("*", end=" ")
     print () 

Output: Enter no. of rows : 5
*
**
* * * *
* * * * *
Example 3: Write a program to display *’s in pyramid style.
पिरामिड स्टाईल में * प्रदर्शित करने के लिए एक प्रोग्राम लिखें।

>
n = int(input ("Enter no.of rows : ")) 
for i in range (1, n+1):
  print (" "* (n-i), end=" ") 
  print("* "  * i)

Output:
        *
      * *
    * * *
  * * * * *
* * * * * *
Example 4: Write a program to display *’s below form.
निम्नलिखित फार्म में * प्रदर्शित करने के लिए एक कार्यक्रम लिखें।
                   *
                * *
               ***
             ****
           *****

n = int (input ("Enter no.of rows :")) 
for i in range(1, n+1):
   print(" "* (n-i),end="") 
   print ("*" * i)

Example 5: Write a program to display * in ‘N’ style.
‘N’ स्टाईल में * प्रदर्शित करने के लिए एक प्रोग्राम लिखें।

for row in range (6): 
    for col in range (6): 
        if col==0 or col ==5 or (row == col and col>0 and col<5):
            print("*",end="") 
        else:
            print (end=" ") 
    print()

Output:
*             *          
*  *         *        
*    *       *          
*       *    *          
*         *  *           
*             *              

for loop with else clause
ईल्स क्लाज के साथ फॉर लूप

In Python, for loop can have an optional else block as well. The else part is executed if the items in the sequence used in for loop exhausts.

पॉयथन में, फॉर लूप के पास एक वैकल्पिक else ब्लॉक भी हो सकता है। else भाग एक्सिक्यूट होता है यदि सिक्वेन्स में आइटम है फॉर लूप का उपयोग करने के लिए।

Syntax:
for<variable> in <sequence>:
statement(s)
else:
statement(s)
Example:

for i in range (5):
  print (i, sep=", ") 
else:
  print("for loop over")

Output:
0
1
2
3
4
for loop over

break statement
ब्रेक स्टेटमेंट

A statement that alters the execution of a loop from its designated sequence is a loop control statement. To break out from a loop, you can use the keyword “break”.

एक स्टेटमेंट जो अपने निर्दिष्ट सिक्वेन्स से एक लूप के एक्जिक्यूसन को बदल देता है, एक लूप कंट्रोल स्टेटमेंट है। लूप से बाहर निकलने के लिए, आप “ब्रेक” कीवर्ड का उपयोग कर सकते हैं।

The break statement in Python terminates the current loop and resumes execution at the next statement, just like the traditional break found in C.

पॉयथन में ब्रेक स्टेटमेंट वर्तमान लूप को समाप्त करता है और C में पाए जाने वाले पारंपरिक ब्रेक की तरह ही अगले स्टेटमेंट पर अमल करना शुरू करता है।

The most common use for break is when some external condition is triggered requiring a hasty exit from a loop. The break statement can be used in both while and for loops.

ब्रेक के लिए सबसे आम उपयोग तब होता है जब कुछ बाहरी कंडीशन को एक लूप से जल्दबाजी में बाहर निकलने की आवश्यकता होती है। ब्रेक स्टेटमेंट का उपयोग दोनों व्हाईल और फॉर लूप किया जा सकता है।

You’ll put the break statement within the block of code under your loop statement, usually after a conditional if statement.

आप अपने लूप स्टेटमेंट के तहत कोड के ब्लॉक के भीतर ब्रेक स्टेटमेंट डालते हैं, जो आमतौर पर कंडीशनल ईफ स्टेटमेन्ट के बाद होता है।

Syntax:
break
Example 1: Write a program”use of break statement inside while loop”
एक प्रोग्राम लिखें “व्हाईल लूप के अंदर ब्रेक स्टेटमेंट का उपयोग करते हुए।”

i = 0 
while i<=10:
   print ("i value :", i) 
   if (i==4):
       break 
   i = i + 1 
print ("After while loop")

Output:
i value : 0
i value : 1
i value : 2
i value : 3
i value : 4
After while loop

continue Statement
कंटिन्यू स्टेटमेंट

The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.

पॉयथन में कांटिन्यू स्टेटमेंट कन्ट्रोल को लूप की शुरुआत में लौटाता है। कांटिन्यू स्टेटमेंट लूप के वर्तमान इटेरेशन में सभी शेष स्टेटमेन्टो को रिजेक्ट करता है और कन्ट्रोल को लूप के शीर्ष पर वापस ले जाता है।

To continue in a loop, you can use the keyword “continue”. The continue statement can be used in both while and for loops.

लूप में कंटिन्यू करने के लिए, आप “continue” कीवर्ड का उपयोग कर सकते हैं। “continue” स्टेटमेन्ट का उपयोग व्हाईल और फॉर लूप दोनों में किया जा सकता है।

Syntax:
continue
Example 1:

for a in 'Python':      # First Example 
   if a == 'h':
      continue 
   else:
      print ('Current Letter :', a) 
print ("Good bye!")

Output:
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Good bye!

Chapter wise Model Paper Link

About Me