THE WORLD'S LARGEST WEB DEVELOPER SITE

Python range() Function

❮ Built-in Functions


Example

Create a sequence of numbers from 0 to 5, and print each item in the sequence:

x = range(6)
for n in x:
  print(n)
Run example »

Definition and Usage

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.


Syntax

range(start, stop, step)

Parameter Values

Parameter Description
start Optional. An integer number specifying at which position to start. Default is 0
stop Optional. An integer number specifying at which position to endt.
step Optional. An integer number specifying the incrementation. Default is 1

More Examples

Example

Create a sequence of numbers from 3 to 5, and print each item in the sequence:

x = range(3, 6)
for n in x:
  print(n)
Run example »

Example

Create a sequence of numbers from 3 to 20, but increment by 2 instead of 1:

x = range(3, 20, 2)
for n in x:
  print(n)
Run example »

❮ Built-in Functions