2.5. Iteration Statements
Iteration statements, also known as loops, are used to iteratively execute a certain piece of code. There are two main types of iteration statements in Python.
a.For loop
b.While Loop
For Loop
The for loop is used to iteratively execute a piece of code for a certain number of times. You should use for loop when you exactly know the number of iterations or repetitions for which you want to run your code. A for loop iterates over a collection of items. In the following example, we create a collection of five integers using the range() method. Next, a for loop iterates five times and prints each integer in the collection.
Script 12:
1. items = range(5)
2. for item in items:
3. print(item)
Output:
0
1
2
3
4
While Loop
The while loop keeps executing a certain piece of code unless the evaluation condition becomes false. For instance, the while loop in the following script keeps executing unless variable c becomes greater than 10.
Script 13:
1. c = 0
2. while c...