for loop iterates over any sequence. Another use case for a for-loop is to iterate some integer variable in increasing or decreasing order. Such a sequence of integer can be created using the function.
The range() function in Python is often used in for statements to define the number of loop iterations. This built-in function creates lists containing arithmetic progressions. The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.
Example - 1:
for x in range(4): print(x)
range(4) is not the values from 0 to 4, but the values from 0 to 3.
Example - 2:
for i in range(1, 5): print ('This is the', i, 'iteration.')
This is the 1 iteration. This is the 2 iteration. This is the 3 iteration. This is the 4 iteration.
Example - 3:
arr_list = ['Mysql', 'Mongodb', 'PostgreSQL', 'Firebase'] for i in range(len(arr_list)): print(arr_list[i], end = ',')
In above example we have used len(arr_list) as the stop value. The for loop will iterate till the stop value i.e the length of the array and that will be 4, as we have four items in the arr_list.