Description:

Repetition is a statement or several statements that can be executed once or multiple times. The number of times is limited by a condition. Sometimes the repetition is forever and this is called infinite loop. There are 2 kind of repetition statements in Python: “while” and “for”.

The “while”statement

The while statement is used for repeated execution as long as an expression is true:

while <logical expression>:
    <statement suite>
else:
    <statement suite>

This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false the suite of the else clause is executed and the loop terminates. The second suite is optional and may not be present.

 

A break statement executed in the first suite terminates the loop without executing the else clause’s suite. A continue statement executed in the first suite skips the rest of the suite and goes back to testing the expression.

For example:

#This example demonstrate an infinite loop
v = 0
while True:
    print("forever young")
    v+=1
    if v==100: break
print("I was young forever")

The previous examples will print 100 times “forever young”, then will print “I was young forever”. So the breck statement has stopped the infinite loop.

The “for” statement

The for statement in Python differs from what you may be used to in Java or C or Pascal. Rather than always iterating over an arithmetic progression of numbers,  Python iterates over the items of any collection of items.

# Measure some strings in a list of strings:
words = ['I', 'like', 'to', 'learn', 'python']
for word in words:
     print(len(word), word)

Will print:

1 I 
4 like 
2 to 
5 learn 
6 python

The “range” function

If you need to iterate over a finite sequence of numbers, the built-in function range() comes in handy. It generates arithmetic progressions that can be used as a collection of numbers:

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

The break and continue

Forcing a repetition block to break or to continue is possible in a for loop or in a while loop. This can be done in a combination with an if statement.

v = 0
for i in range(1,10):
      if i < 5:
         continue
      if i == 7:
         break
      v = i
print("v = ", v)

Will print:

v = 6

This example has demonstrate the for loop, the continue and the break.

The “else” in for and while

The official declaration of for loop statement is difficult to read but we can see the for statement also have an “else” part like the while loop.

for_stmt ::=  "for" target_list "in" expression_list ":" suite
              ["else" ":" suite]

This means you can write something like this:

for i in range(10):
    print('i=',i)
    if i>=3: break
else:
    print('finish i=',i)
i=0
i=1
i=2
finish i=3

Note:The else  is executed when the range is finished or break is used. However not many developers knows about this feature. It can be useful to improve the program readability if the loop is very long.

 

Read next: Data types