# Python range() Function

Posted

•3 min read

The Python `range`

type generates a sequence of integers by defining a start and the end point of the range. It is generally used with the `for`

loop to iterate over a sequence of numbers.

`range()`

works differently in Python 2 and 3.

In Python 2, there are two functions that allow you to generate a sequence of integers, `range`

and `xrange`

. These functions are very similar, with the main difference being that `range`

returns a list, and `xrange`

returns an xrange object.

In Python 3, the `xrange`

function has been dropped, and the `range`

function behaves similarly to the Python 2 `xrange`

. Python 3 `range`

is not a function but rather a type that represents an immutable sequence of numbers.

In this article, we will cover the basics of the Python 3 `range`

type.

## Python `range()`

syntax

The `range`

constructor takes the following forms:

```
range(stop)
range(start, stop[, step])
```

The arguments provided to the `range`

constructor must be integers. Floating numbers and other types are not allowed.

`range`

takes one required and two optional arguments. It returns a range object that represents the given range and generates the numbers on demand.

## Python `range(stop)`

When only one argument is given, `range`

returns a sequence of numbers, incremented by `1`

, starting from `0`

to `stop - 1`

.

Here’s the range type in action:

```
for i in range(5):
print(i)
```

The generated sequence of numbers starts from `0`

and ends with `4`

(5-1):

```
0
1
2
3
4
```

If the argument is `0`

or a negative integer `range`

returns an empty sequence:

```
print(list(range(-5)))
```

We’re converting the range object to a list because range does a lazy evaluation of the integer sequence. The output is an empty list:

```
[]
```

## Python `range(start, stop)`

When two arguments are provided, `range`

returns a sequence of numbers, incremented by `1`

, starting from `start`

to `stop - 1`

.

Here is an example:

```
for i in range(3, 5):
print(i)
```

```
3
4
```

The `stop`

argument must be greater than `start`

. Otherwise, the sequence is empty:

```
print(list(range(5, 3)))
```

```
[]
```

You can use `0`

, positive and negative integers as arguments:

```
print(list(range(-5, -3)))
```

```
[-5, -4]
```

```
print(list(range(-3, 0)))
```

```
[-3, -2, -1]
```

## Python `range(start, stop, step)`

When three arguments are given, `range`

returns a sequence of numbers, incremented or decremented by `step`

, starting from `start`

to `stop - 1`

.

If `step`

is positive, `range`

returns a sequence that increments:

```
for i in range(0, 26, 5):
print(i)
```

```
0
5
10
15
20
25
```

When incrementing, the `stop`

argument must be greater than `start`

. Otherwise, the sequence is empty.

If `step`

is negative, `range`

returns a sequence that decrements:

```
for i in range(20, 4, -5):
print(i)
```

```
20
15
10
5
```

When decrementing, the `stop`

argument must be less than `start`

. Otherwise, the sequence is empty.

If `step`

is `0`

a ValueError exception is raised:

```
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: range() arg 3 must not be zero
```

## Conclusion

The Python `range`

type allows you to generate a sequence of integers. It is mostly used in `for`

loops.

If you have any questions or feedback, feel free to leave a comment.