Python Loops

loops in python

Python Loops : In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. There may be a situation when you need to execute a block of code several number of times.

Programming languages provide various control structures that allow for more complicated execution paths.

A loop statement allows us to execute a statement or group of statements multiple times. The following diagram illustrates a loop statement −


Types of Loops

Python programming language provides following types of loops to handle looping requirements.

Python For Loops

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.

With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

Syntax: 

for var in iterable:
    # statements

Example:

str = “AskAtul”  
for i in str:
    print(i)  


Output:

A
s
k
A
t
u
l

Python While Loop

In Python, While Loops is used to execute a block of statements repeatedly until a given condition is satisfied. And when the condition becomes false, the line immediately after the loop in the program is executed. 

Syntax

The syntax of a while loop in Python programming language is −

while expression:
   statement(s)

Example:

  1. i=1  
    #The while loop will iterate until condition becomes false.  
    While(i<=10):    
       print(i)       i=i+1   

Output:

1
2
3
4
5
6
7
8
9
10

Python nested Loop

Python programming language allows to use one loop inside another loop. Following section shows few examples to illustrate the concept.

Syntax

for iterating_var in sequence:
   for iterating_var in sequence:
      statements(s)
   statements(s)

The syntax for a nested while loop statement in Python programming language is as follows −

while expression:
   while expression:
      statement(s)
   statement(s)

A final note on loop nesting is that you can put any type of loop inside of any other type of loop. For example a for loop can be inside a while loop or vice versa.

Example

The following program uses a nested for loop to find the prime numbers from 2 to 100 −

#!/usr/bin/python

i = 2
while(i < 100):
   j = 2
   while(j <= (i/j)):
      if not(i%j): break
      j = j + 1
   if (j > i/j) : print i, " is prime"
   i = i + 1

print "Good bye!"

Output:


2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime
Good bye!

Loop Control Statements

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements. 
 

Python Continue statement

It returns the control to the beginning of the loop.

Syntax

#loop statements    
continue  
#the code to be skipped 

Example:

i = 0                     
while(i < 10):                
   i = i+1  
   if(i == 5):  
   continue  
   print(i) 

Output:


1
2
3
4
6
7
8
9
10

Python break statement

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

Syntax

The syntax for a break statement in Python is as follows −

break

Example

#!/usr/bin/python

for letter in 'Python':     # First Example
   if letter == 'h':
      break
   print 'Current Letter :', letter
  
var = 10                    # Second Example
while var > 0:              
   print 'Current variable value :', var
   var = var -1
   if var == 5:
      break

print "Good bye!"

Output:

Current Letter : P
Current Letter : y
Current Letter : t
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Good bye!

Recommended Posts:

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *