Python looping
Not only in Python but in many other languages looking is the most important concept which plays an imperative role for any programming language. Looping is the terminology which mean of occuring a statement again and again according to the syntax of loop.
loop statement gives us facility us to execute a press release or group of statements multiple times consistent with the logic.
Following are types of looping statement in Python:
1.for loop
In this type of looping we can count the number of statement to be Run in for loop. We can exactly trace that at least how many time the block of loop will run.
Example:
for alphabat in 'fruits'
print ' alphabet is :', alphabet
fruits = ['banana', 'apple', 'mango']
for word in fruits:
print 'word is :', fruit
print "happy codding !"
2. while loop
In while loop we cannot predict that loop will run for one time also. The syntax of while loop is completely different from for loop.
While loop is executed as long as the condition is true, then it will execute the given statement. The condition will be true in a non zero value. Here indentation concept should be kept in mind while making a code and proper segmentation of spaces should be adopted.
Example
Num = 0
while (Num < 9):
print 'The number is:', Num
Num = Num + 1
print "Good bye!"
3. nested loop
Nested loop is just like nested if else.
We can place one loop inside another loop, therefore several loop can be coded with another loop. So we can defined nested loop if I use one loop inside another loop.
Following program to illustrate more on nested loop.
a = 2
while(i < 100):
b = 2
while(b <= (a/b)):
if not(a%b): break
b = b + 1
if (b > a/b) : print i, " is prime"
a = b+ 1
print "happy codding !"
