Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

slice

slice creates a reusable object describing a range of indices. Sequences can consume it for indexed access, and Python also creates such objects from slicing syntax.

How it works

slice constructs an object that describes a range of indices in the same way as range(start, stop, step). The object stores start, stop, and step; sequence types can interpret it for indexed access, and slicing syntax creates equivalent objects internally.

Syntax

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

Parameters

NameTypeDescription
startobjectBeginning index; defaults to None.
stopobjectEnding index or required range endpoint.
stepobjectStride between indices; defaults to None.

Returns

A slice object representing the indices described by the arguments.

Key points

  • The result is a reusable description of an index range rather than the elements selected from a particular sequence.
  • Its three bounds are available through read-only attributes, which makes the object inspectable after construction.
  • Those attributes preserve exactly the values passed to the constructor, including defaults.
  • Python’s slicing and extended-indexing notation can create slice objects implicitly.

Common mistakes

  • Omitting start or step when constructing the object gives it the value None; the receiving sequence determines how those bounds are interpreted.
  • The a[start:stop:step] notation creates slice semantics automatically, so explicitly calling slice is mainly useful when storing or reusing the bounds.

Example: Reuse a slice object

values = [0, 1, 2, 3, 4, 5]
window = slice(1, 5, 2)
print(values[window])
print(window.start, window.stop, window.step)

A slice object can be reused for indexed access and exposes the three arguments it stores.

When to use it

Reuse indexing rules

Apply identical bounds to multiple sequences or repeatedly access one sequence without duplicating slicing expressions.

Represent dynamic ranges

Pass start, stop, and step as data to code that performs indexing later.

Handle slicing metadata

Inspect or validate bounds through the object’s read-only attributes before applying them.

Interview questions

QWhat does the built-in slice do?

slice creates a slice object representing indices specified by range(start, stop, step). It does not itself return the selected elements; a sequence uses the object during subscription.

Follow-up: How can the object’s bounds be inspected? Read its start, stop, and step attributes; these are read-only and contain the supplied values or defaults.

QWhat is the signature and which arguments have defaults?

The signature is slice(start, stop, step=None). start and step default to None, while stop is the required endpoint in the documented constructor form.

Follow-up: What syntax creates an equivalent object without calling slice? Extended slicing such as a[start:stop:step] generates a slice object internally.

QWhich attributes does a slice object provide?

A slice object stores three read-only data attributes: start, stop, and step. They expose the argument values, including None when an argument used its default.

Follow-up: Does the object select data by itself? No; it describes indices, and the indexed object or an extension interprets that description.

  • range
  • object.__getitem__
  • __index__

Source: Built-in functions. Checked 2026-10-16.