On this page
range
Prev: quit | Up: __builtins__ | Next: repr
Rel: bool, int, float, complex, str
Ref: docs.python.org — range
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import range
Overview & Signature
range(stop) -> range object
range(start, stop[, step]) -> range object Return an object that produces a sequence of integers from start (inclusive) to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1.
Common Methods & Attributes
range.count(self, object, /)— rangeobject.count(value) -> integer -- return number of occurrences of valuerange.index(self, object, /)— Raise ValueError if the value is not present.range.start(...)—range.startmethod or attribute.range.step(...)—range.stepmethod or attribute.range.stop(...)—range.stopmethod or attribute.
Usage
assert list(range(5)) == [0, 1, 2, 3, 4]
assert list(range(10, 0, -3)) == [10, 7, 4, 1]
assert 500_000 in range(1_000_000) # O(1) membership test!
Gotchas
- Lazy $O(1)$-memory sequence, NOT a list: Supports indexing, slicing,
len(), and $O(1)$inchecks without allocating all numbers. - Stop is exclusive and step must be a non-zero
int.
REPL Inspect
help(range)
Linked from
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/quit Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/repr Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bool Linked here; shared tags: #builtins
- tuts/python3/__builtins__/complex Linked here; shared tags: #builtins
- tuts/python3/__builtins__/float Linked here; shared tags: #builtins
- tuts/python3/__builtins__/int Linked here; shared tags: #builtins
- tuts/python3/__builtins__/str Linked here; shared tags: #builtins
- tuts/python3/tutorials/core_fundamentals/control_flow links to this page