Like any other language python also has loops to iterate or repeat some work with some condition evaluation, this make any language quit useful to solve various problems where we need to check or work on every element of variable sequence. variable sequence could be anything string, int, list tuple or dictionary.

Python Programming language provide below types loops for iterate requirement problems.

  • for loop
  • while loop
  • nested while or for loops

let’ see all of them mentioned above one by one with some example and discussion on same.

for loop

Python for loop is same as any other programming language, Just same syntax rules as it was for conditions if statements. let’s see some simple and effective example.

>>> for i in "Python":
...  print i
... 
P
y
t
h
o
n

Through this example we print all index elements in Python word in new line. but its just an example we could do some useful stuff with same for loop, before create another example we should aware some basic rules of syntax while writing in python for loop

  • Every code statement after loop header statements should have proper indentation
  • colon (:) after Every loops statement header statement like after for , while etc

As now we know some basic syntax rules for loops, we could write some useful codes to describe for loop.

>>> def tables(a):
...     for i in range(1,11):
...             print i,"X ",a, "= ", i*a
... 
>>> tables(3)
1 X  3 =  3
2 X  3 =  6
3 X  3 =  9
4 X  3 =  12
5 X  3 =  15
6 X  3 =  18
7 X  3 =  21
8 X  3 =  24
9 X  3 =  27
10 X  3 =  30

in Above code, we have created one program which could able to calculate 10 Times multiples of any number.

While Loop

In while loop, we could iterate some operations until provided conditions evaluate true. sometime this also used to start infinite loops. let’s try one.

>>> count=1
>>> while True:
...  print count
...  count +=1
... 

once we write this code and press enter, it will print number and increase continually. we need to add some conditions to stop such iteration.

>>> while True:
...  print count
...  count +=1
...  if count == 11:
...   break
... 
1
2
3
4
5
6
7
8
9
10

See break statement if good example to stop any iteration once certain conditions reach during code. we can even modify while statement in such way we need not to write break keyword.

>>> count=0
>>> while count < 10:
...  print count
...  count+=1
... 
0
1
2
3
4
5
6
7
8
9

In above code we already include condition in while statement which make it more suitable for understanding in first place.

Same as break statement, we have one continue statement, which will not exit code but skip below code in same condition iteration statement either for or while and start it again from condition evaluation.

>>> while count < 10:
...  if count == 5:
...   count+=1
...   continue
...  print count
...  count+=1
... 
1
2
3
4
6
7
8
9

See above , we have one code which display number from 1 to 4 , skip print 5 and then again print till 9.Here we skip print 5 through continue statement.

We could also write many nested for or while loop within previous loop which suitable to solve complex problems.

>>> for i in range(1,5):
...     print "inner--",i
...     for i in range(i,5):
...             print "outer--",i
... 
inner-- 1
outer-- 1
outer-- 2
outer-- 3
outer-- 4
inner-- 2
outer-- 2
outer-- 3
outer-- 4
inner-- 3
outer-- 3
outer-- 4
inner-- 4
outer-- 4

I understand although there is no use of it. but enough to understand how nested loops works. we will see some suitable examples in later post when start practice some real code in python