Python programming language provides the following types of loops. Here we cover all the ways for executing the loops. While all the ways provide similar basic functionality, the difference in there syntax and condition checking the time .
1. For loop
A for loop is used for iterating over a sequence that is string, tuple, dictionary , a set or a list. With the help of for loop, we execute a set of a statement once from in each item in a list, tuple and a set, etc. Let’s take an example.
The output of the following for loop program is :
Let’s take another example. Here we cover range function in for loops.
1. for x in range(7) : print(x) # Note that it’s value is not 0 to 6 . Its value is 0 to 5 .
2. for x in range(2,8) : print(x) # It’s cover 2, 3 , 4 ,5 ,6 ,7 ,8
Let’s take this example in Pycharm idle :
The output of the following program is :