Skip to main content

Python for loop with range() function

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):



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 = ',')

Output: Mysql,Mongodb,PostgreSQL,Firebase,

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.

Submitted by devanshi.srivastava on June 7, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.


At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.