slice() Slice object

Last updated: 2026-09-22

slice() Slice object

slice(stop) or slice(start, stop[, step]) returns a slice object used for __getitem__.

CategoryBuilt-in Functions
KindBuilt-in Function
Python Versionall

📝 Syntax

slice(stop)
slice(start, stop[, step])

⚙️ Parameters

stop RequiredThe stop index (exclusive); in the one-arg form it is the slice endpoint.
start OptionalThe start index (inclusive); None when omitted.(Default:None)
step OptionalThe step; None when omitted.(Default:None)

Returns:A slice object with start/stop/step attributes, passable to __getitem__.

💥 Raises

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

[1, 3]
More case studies coming soon — check back later.

❓ FAQ

QIs slice(1, 5, 2) the same as lst[1:5:2]?
AYes — lst[1:5:2] internally calls lst[slice(1, 5, 2)]; a slice object suits dynamically constructed slices (e.g. when the slice parameters come from config or user input).
QMust the start/stop/step of a slice object always be integers?
ANo, they can be omitted as None: the start and step of slice(5) are both None, meaning 'from the start up to index 5', consistent with lst[:5].
QHow does a custom class support slicing?
AImplement __getitem__(self, key); when key is a slice object, parse its start/stop/step and apply the corresponding logic.
QMust slice arguments be integers?
AYes — passing a non-integer (e.g. slice('a', 'b')) raises a TypeError; the arguments must implement __index__.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏