Python Loops

Python Loops are one of the basic concepts in programming. In programming, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. There can be a situation where you need to execute a block of code several times. A loop statement is used for this purpose.

The figure below shows the python loops,

Here are some types of Python loops statement with descriptions,
Loop TypesDescription
while loopRepeats a statement or group of statements while a given condition is
TRUE. It tests the condition before executing the loop body.
for loopExecutes a sequence of statements multiple times and
abbreviates the code that manages the loop variable.
nested loopsYou can use one or more loops inside any other while, for, or do..while loop.
Below is the detailed description of each type.

While Loop:

In Python, While Loop is used to run a block of statements again and again until the given condition is true. And if the condition is false, after execution there shows a line after the loop in the program.

Syntax,

#python while loop

count = 0
while (count < 5):
   print 'The value is:', count
   count = count + 1

print "No Value"

Using else Statement with While Loop:

In Python, some programs support having an else statement associated with a loop statement. If the else statement is used with a while loop, the else statement is executed when the condition becomes false.

#program using else with while

count = 0
while count < 3:
   print count, " is  less than 3"
   count = count + 1
else:
   print count, " is not less than 3"

For Loop:

In Python For Loop has the ability to iterate the items of any sequence, such as a list or a string.

Syntax,
#python for loop
 
n = 6
for i in range(0, n):
    print "Value is :",  n

Using else Statement with For Loop:

In Python, some programs support having an else statement coordinate with a loop statement. If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list.

#for loop with else loop

list = ["Learn", "Python", "Programming"]
for index in range(len(list)):
    print list[index]
else:
    print "Inside code Block"

Nested Loop:

Python programming language allows to use one loop inside another loop. Given below shows the example for this one

Syntax,
#nested loop

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

print "End list"

Loop Control Statements:

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

Here are the loop control statements with their description ,
Control statementsDescription
Break Statement
Terminates the loop statement and transfers execution
to the statement immediately following the loop.
Continue StatementCauses the loop to skip the remainder of its body and
immediately retest its condition prior to reiterating.
Pass StatementThe pass statement in Python is used when a statement is required
syntactically but you do not want any command or code to execute.

Break statement:

#break statement

for letter in 'Python Break Statement': 
 
    #break the loop as soon it sees 'e' 
    #or 's'
    if letter == 'e' or letter == 's':
         break
 
print 'Current Letter :', letter

Continue statement:

#continue statement
#Prints all letters except 'n' and 'e'
for letter in 'Continue statement': 
    if letter == 'n' or letter == 'e':
         continue
    print 'Current Letter :', letter
    var = 10

Pass statement:

# An empty loop
for letter in 'Pass statement':
    pass
print 'Last Letter :', letter