KKAIROS
Practical guideIllustrative3 min read

Python range: a sequence of numbers

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.

Published:

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.

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

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

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:

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):

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

Command:

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):

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

Command:

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.

Ad

Source

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.

All full guides: All guides