# Python range: a sequence of numbers

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-range-full-en
`range(stop)` counts from 0 up to but not including `stop`. `range(start, stop, step)` sets the start and the step. Use `list(range(...))` to see the numbers.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-97-A, TE-97-B and TE-97-C. The expected non-zero exit is TE-97-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-97-A: range with one, two and three arguments, and a negative step

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
print(list(range(3)))
print(list(range(2, 5)))
print(list(range(0, 10, 3)))
print(list(range(3, 0, -1)))
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
[0, 1, 2]
[2, 3, 4]
[0, 3, 6, 9]
[3, 2, 1]
```

Exit status: 0. The output matches the expected text written before the run.

### TE-97-B: a range object has a length, can be indexed, and answers membership tests without being turned into a list

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
r = range(2, 8, 2)
print(len(r), r[1], 4 in r, 5 in r)
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
3 4 True False
```

Exit status: 0. This status and the output match what was written before the run.

### TE-97-C: expected non-zero exit: a step of 0 raises ValueError

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
range(0, 5, 0)
EOS
```

Command:

```bash
python3 s.py
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
Traceback (most recent call last):
  File "/tmp/b24-9w3cyf_l/work/s.py", line 1, in <module>
    range(0, 5, 0)
ValueError: range() arg 3 must not be zero
```

Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.

TE-97-B: range(2, 8, 2) has length 3, its item at index 1 is 4, 4 is in it and 5 is not. TE-97-C: range(0, 5, 0) stopped with exit status 1. The last line of standard error is ValueError: range() arg 3 must not be zero.

## Limits

Only positive and negative integer steps were run. A step of 0 raises ValueError according to the docs, and TE-97-C runs it. Not tested on this page: the `count` method (the number of occurrences of a value) and the `index` method (the position of a value, with a ValueError when the value is absent) of a range object, and the `reversed` iterator. The help text of Python 3.10.12 lists these methods. No run here called them.

## Source

- https://docs.python.org/3/library/stdtypes.html#range

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-97-A` | range with one, two and three arguments, and a negative step | 0 | PASS | `597b0de4668d` |
| `TE-97-B` | a range object has a length, can be indexed, and answers membership tests without being turned into a list | 0 | PASS | `fa95cd10412b` |
| `TE-97-C` | expected non-zero exit: a step of 0 raises ValueError | 1 | PASS | `012219d70aa8` |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC.
Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Python any and all: test a whole list](https://kairos-2z6s.onrender.com/res/python-any-all-full-en)
- [Python isinstance: check a type](https://kairos-2z6s.onrender.com/res/python-isinstance-full-en)
- [Python re module: find, extract and replace text with regex](https://kairos-2z6s.onrender.com/res/python-re-full-en)
- [Python Raise: signal an error on purpose](https://kairos-2z6s.onrender.com/res/python-raise-full-en)
- [Python round: ties go to the even number](https://kairos-2z6s.onrender.com/res/python-round-full-en)
- [Python Zip: loop over lists together](https://kairos-2z6s.onrender.com/res/python-zip-full-en)
