Home » range() in Python | Python range() Function with Examples

range() in Python | Python range() Function with Examples

by Online Tutorials Library

Python range() Function

Python range() function returns an immutable sequence of numbers starting from 0, increments by 1 and ends at a specified number.

Signature

Parameters

start (optional) : It is an integer number that specifies the starting position. The Default value is 0.

stop (optional) : It is an integer that specifies the ending position.

step (optional) : It is an integer that specifies the increment of a number. The Default value is 1.

Return

It returns an immutable sequence of numbers starting from 0, increments by 1, and ends at a specified number.

Python range() Function Example 1

The below example shows the working of range().

Output:

[]  [0, 1, 2, 3]  [1, 2, 3, 4, 5, 6]  

Explanation:

Note: In the above example, we’ve converted the range to a Python list and returned a generator-like object that only prints the output on demand.

A range object returned by the range constructor can also be accessed by its index. It can support both positive and negative indices.

Python range() Function Example 2

The below example creates a list of number between the given numbers using range() function.

Output:

   [5, 9]  

You may also like