Python Loops

for loop:

for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.

Iterate list using for loop

words = ['one', 'two', 'three']                                                 
for w in words:                                                                 
   print(w, len(w))
Output:
('one', 3)
('two', 3)
('three', 5)

Iterate list using for loop in limits

words = ['one', 'two', 'three']                                                 
for w in words[1:2]:                                                            
    print(w, len(w))
Output:
('two', 3)

for loop with enumerate method

enumerate method can be used with for loop to get index of element in list and element value pair in looping.
for i, ch in enumerate(["a", "b", "c", "d"]):
    print i, ch
Output:
0 a
1 b
2 c
3 d

range(x):

Range method is used to generate the list with element 0 to (x-1) number and iterates over a sequence of numbers.

Range method creates list first.

print range(5);
Output:
[0, 1, 2, 3, 4]

Iterate Range method

for i in range(4):
    print(i)
Output:
0
1
2
3

Iterate range method with different start value

here starting value for list element is 2 but default is 0 if not provided.
for i in range(2, 4):
    print(i);
Output:
2
3

Iterate range method with step increment value

range method allows to generate list with different step increment sequence but default step increment value is 1 and optional.
# range with step increment value is 2
for i in range(2, 6, 2):
    print(i)
Output:
2
4
 
print range(1, 10, 2);
Output:
[1, 3, 5, 7, 9]

xrange method

xrange is similar like range method but difference xrange does not generate list elements before iteration, during iteration xrange generate elements.
print xrange(5);
Output:
xrange(5)

iterate xrange method

for i in xrange(4):                                                             
    print(i);
Output:
0
1
2
3

xrange method with step increment value

for i in xrange(2, 10, 3):                                                      
    print(i);  
Output:
$ python test_xrange.py
2
5
8

while loop:

Executes the code in 'while block' multiple times till condition fails.
loop_index = 0
while loop_index < 4:
    print(loop_index)
    loop_index = loop_index + 1
 
Output:
0
1
2
3
 

break:

break is used to exit a for loop
loop_index = 0
while loop_index < 4:
    print(loop_index)
    if loop_index == 2:
        break
    loop_index = loop_index + 1
    
Output:
0
1
2
for i in range(8):
    print(i)
    if i == 3:
        break;
Output:
0
1
2
3

continue:

continue is used to skip the current block, and return to the loop condition.
loop_index = 0
while loop_index < 4:
    print(loop_index)
    if loop_index == 2:
        continue
    loop_index = loop_index + 1
    
Output:
0
1
2
2
3

Iterate range with continue statement

for i in range(6):
    if i == 3:
        continue;
     print(i);
Output:
0
1
2
4
5
6

else with loop:

else can be used with loop statement. When the loop condition statement fails then code block in 'else' will be executed.
If break statement is executed inside the loop then the 'else' block will be skipped.
'else' block will be executed for continue statement is executed in the loop block.
loop_index = 0
while loop_index < 4:
    print(loop_index)
    loop_index = loop_index + 1
else:
    print("loop index is %d" %(loop_index))
    
Output:
0
1
2
3
 

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^