On this page
slice
Prev: setattr | Up: __builtins__ | Next: sorted
Rel: bool, int, float, complex, str
Ref: docs.python.org — slice
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import slice
Overview & Signature
slice(stop)
slice(start, stop[, step]) Create a slice object.
Common Methods & Attributes
slice.indices(self, object, /)— Assuming a sequence of length len, calculate the start and stop indices, and the stride length of the extended slice described by S. Out of bounds indices are clipped in a manner consistent with theslice.start(...)—slice.startmethod or attribute.slice.step(...)—slice.stepmethod or attribute.slice.stop(...)—slice.stopmethod or attribute.
Usage
last_two = slice(-2, None)
assert [10, 20, 30, 40][last_two] == [30, 40]
assert slice(0, 10, 2).indices(5) == (0, 5, 2)
Gotchas
- Great for naming reusable column/field slices when parsing fixed-width text records.
REPL Inspect
help(slice)
Linked from
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/setattr Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/sorted 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/c/builtins shared tags: #builtins