Built-in Functions and Types

slice()

Package slicing boundaries into an object with read-only start, stop and step attributes. Omitted start and step are None; their actual meaning depends on the sequence and direction. Construction does not itself access a sequence or validate that step is nonzero. Built-in sequence application rejects zero steps. Use indices(length) to normalize integer-like boundaries for a given sequence length.

Formal Syntax & Structure

python syntaxpython
slice(stop, /)slice(start, stop, step=None, /)

Parameters & Attributes

NameTypeDescription
startobject or NoneStart boundary; omitted in single-argument form.
stopobject or NoneExclusive boundary when used with normal sequences.
stepobject or NoneStride; None selects the default.

Return Value

slice

A reusable slicing descriptor; hashable when its components are hashable in Python 3.12 and later.

Working Example

Example Snippetpython
sample = [10, 20, 30, 40, 50]selector = slice(1, None, 2)print(sample[selector])print(selector.indices(len(sample)))
Python Reference