# range#

Function `range` returns an immutable sequence of numbers as a `range` object.

Function syntax:

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

Parameters of function:

• start - from what number the sequence begins. By default - 0

• stop - on which number the sequence of numbers ends. Mentioned number is not included in range

• step - with what step numbers increase. By default 1

Function `range` stores only start, stop and step values and calculates values as necessary. This means that regardless of the size of range that describes `range` function, it will always occupy a fixed amount of memory.

The easiest `range` option is to pass only stop value:

```In : range(5)
Out: range(0, 5)

In : list(range(5))
Out: [0, 1, 2, 3, 4]
```

If two arguments are passed, the first is used as start and the second as stop:

```In : list(range(1, 5))
Out: [1, 2, 3, 4]
```

And in order to indicate sequence step, you have to pass three arguments:

```In : list(range(0, 10, 2))
Out: [0, 2, 4, 6, 8]

In : list(range(0, 10, 3))
Out: [0, 3, 6, 9]
```

Function `range` can also generate descending sequences of numbers:

```In : list(range(10, 0, -1))
Out: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

In : list(range(5, -1, -1))
Out: [5, 4, 3, 2, 1, 0]
```

To get a descending sequence use a negative step and specify start by a greater number and stop by a smaller number.

In descending sequence the steps can also be different:

```In : list(range(10, 0, -2))
Out: [10, 8, 6, 4, 2]
```

Function supports negative start and stop values:

```In : list(range(-10, 0, 1))
Out: [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]

In : list(range(0, -10, -1))
Out: [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
```

The `range` object supports all operations that support sequences in Python, except addition and multiplication.

Check whether a number falls within a range:

```In : nums = range(5)

In : nums
Out: range(0, 5)

In : 3 in nums
Out: True

In : 7 in nums
Out: False
```

Note

Starting with Python 3.2 this check is performed in constant time (O(1)).

You can get a specific range element:

```In : nums = range(5)

In : nums
Out: 0

In : nums[-1]
Out: 4
```

Range supports slices:

```In : nums = range(5)

In : nums[1:]
Out: range(1, 5)

In : nums[:3]
Out: range(0, 3)
```

You can get range length:

```In : nums = range(5)

In : len(nums)
Out: 5
```

And a minimum and maximum element:

```In : nums = range(5)

In : min(nums)
Out: 0

In : max(nums)
Out: 4
```

In addition, `range` object supports `index` method:

```In : nums = range(1, 7)

In : nums.index(3)
Out: 2
```