Loops, also known as iteration statements, are used to repeatedly execute a particular piece of code.
while loops are the two iteration statements that the Python programming language supports. In this article, you will see how to implement Python
This tutorial will discuss the following points:
for loop in Python iterates over a collection of items. The number of iterations for a
for loop is equal to the number of items in the collection that the
for loop iterates on. Let’s take a look at a very simple example:
ints = [10,20,30,40,50] for i in ints: print(i)
In the script above, we create a list of integer named
ints with 5 items. Next, a
for loop iterates over the
ints list and prints all the items one by one. Notice that the
for loop iterates 5 times, since there are 5 items in the
ints list. The output of the script above is as follows:
10 20 30 40 50
To learn more about Python lists, check out our article on how to use lists in Python.
while loop continues executing the code block until the while condition returns true. It should be used when you want to repeatedly execute a certain piece of code until a particular condition remains true. Let’s see a very simple example of a while loop that prints the first 10 positive integers:
i = 1 while i <= 10: print(i) i = i + 1
1 2 3 4 5 6 7 8 9 10
In the script above we initialize a variable
i with value 1. Next, the while condition checks if the value in the variable
i is less than 10. If the condition returns true, execute the body of the loop else, terminate the loop. Inside the while loop, the value of the variable
i is printed and then the variable is incremented by 1. The while loop terminates once the value of
i variable becomes 11 since the while condition returns false.
To further clarify the concept of the while loop, lets print the multiplication table of
7 using a while loop.
i = 1 while i <= 10: print("7 x ", i, " =", 7*i) i = i + 1
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70
continue statement in Python is used inside iteration statements to skip the code execution that proceeds the
continue statement. It sends the control back to the beginning of a loop and starts the next iteration, without executing the remaining code in the loop for the current iteration.
In the following example, you will see how we can use the
continue statement inside a
for loop to print all the odd numbers between 1 and 20.
ints = range(1, 21) for i in ints: if i%2 != 1: continue print(i)
In the script above, we first create a collection of integers between 1 and 20 inclusive, using the
range(1, 21) function. Next, a
for loop iterates through the collection. For an integer to be odd, the remainder of the integer when divided by 2, should be 1. In the script above, if the remainder of the currently iterated item, when divided by 2, is not equal to one, we simply use the
continue statement to skip the remaining code in the current iteration and execute the next iteration. Else, if the remainder of the item divided by 2 is 1, that means the item is odd, we use the print statement to print the item.
Here is the output of the script above:
1 3 5 7 9 11 13 15 17 19
The break statement when executed inside a loop forcefully terminates the execution of a loop. The remaining code in the current iteration, after the break statement, will no longer be executed and the iteration will end forcefully. Take a look at a very simple example of a
i = 1 while i <= 10: print("7 x ", i, " =", 7*i) i = i + 1 if i == 6: break
In the above script, the
while loop prints the multiplication table of 7. However, when the value of the variable
i becomes 6, the break statement terminates the execution of the loop. In the output, you will only see the first 5 lines of the table as shown below:
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35
In this tutorial, we discussed about
while loops in Python which are the two iteration statements supported by Python. The
for loop iterates over a collection of items whereas the
while loop iterates until the while condition returns true. We also demonstrated the use
continue statements in loops. The continue statement skips the remaining code in the current loop iteration and starts the next loop iteration from the beginning. On the other hand, the break statement terminates the loop altogether.