Table of Contents

About

A range represents a sequence of integer, it is not a list or a set.

To get a list of a set from a range, you have to use the constructor: list() or set().

>>> list(range(2))
[0, 1]

Snippet

For any integer n, range(n) represents the sequence of integers from 0 through n - 1.

for x in range(5):
    print x
0
1
2
3
4

Syntax

A range can take 1, 2 or 3 arguments.

You can form a range with one, two, or three arguments.

  • The expression range(a,b) represents the sequence of integers a, a + 1, a + 2, … , b - 1.
  • The expression range(a,b,c) represents a, a + c, a + 2c, … (stopping just before b).

1 argument

Range starts the range at zero and increments by 1 until the size reaches 1 less than the range.

For instance:

print range(1) 
[0]

print range(2) 
[0,1]

2 arguments

Range starts the range at the first argument and increments by 1 until the size reaches 1 less than the second argument:

range(1,3)
[1,2]

print range(2,5)
[2, 3, 4]

3 arguments

range(Start,End,Increment)

where:

  • Start is the number the list starts at,
  • End is the number minus 1 where the list ends,
  • Increment is how much you should increment by (default increment of 1)

For instance:

print range(4,13,2)
[4, 6, 8, 10, 12]