Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Python's range() Function Explained

2 min read

One of Python’s built-in immutable sequence types is range(). This function is extensively used in loops to control the number of types loop have to run. In simple words range is used to generate a sequence between the given values.

The range() function can take 1 to 3 of the following arguments:

  • Start: The integer value for the origin of the sequence, if not passed implicitly it's 0
  • Stop:  The integer value up to which the sequence will get generated but won't include this number. This is a required argument which must be passed,
  • Step: Establishes the difference between each iteration it can be positive or negative if not given the default step is 1

The order of arguments is as follows, range(start, stop, step). Note that all parameters must be integers however they can be positive or negative.

Using range() Function In Python

Let's start with generating a simple series and printing it out

for i in range(0, 6, 1):
    print(i)

Above for loop will print every number from 0 to 5 maintaining a constant difference 1.

Output:

0
1
2
3
4
5

It's worth mentioning that similar to list indexing in range starts from 0 which means range( j ) will print sequence till ( j-1) hence the output doesn't include 6.

As mentioned earlier the default value of start is 0 and for step it's 1, therefore the below code produces the same output.

for i in range(6):
    print(i)

Output:

0
1
2
3
4
5

Generating a decreasing sequence is also possible by specifying a negative value to step argument.

for i in range(30,0,-5):
    print(i)

Output:

30
25
20
15
10
5

Range function can also be used to iterate over the elements of a list.

li = ["1", "text", "2", "more Text",3,4,5]
for i in range(len(li)):
    print(li[i])

Output:

1
text2
more Text
3
4
5

It's worth noting that in Python 2 the output of the range function was a list, but in python 3 the range() function doesn't produce a list so we can't perform list operation on it, but converting the output to a list is possible using the built-in list() method.

>>> x = list(range(6))
>>> x[0, 1, 2, 3, 4, 5]


PYTHON

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read