The built-in slice()
function creates a slice
object representing a set of indices specified by range(start, stop, step)
. This object can be used to extract portions of sequences like strings, lists, or tuples:
slice()
Signatures Arguments Argument Description Default Value start
The initial index of the slice. None
(start of the sequence) stop
The ending index of the slice, exclusive. Required argument step
The step between each index in the slice. None
(step of 1
) Return Value
slice
object that can be used to extract a portion of a sequence.slice()
Examples
With only a stop
value:
With start
, stop
, and step
values:
slice()
Common Use Cases
The most common use cases for the slice()
function include:
slice()
Real-World Example
Say that you have a list of daily temperatures and want to extract only the temperatures for the first day of each week, skipping weekends:
In this example, slice(0, None, 7)
allows you to select every 7th temperature starting from the first element, effectively picking temperatures for the first day of each week.
Tutorial
Python's Built-in Functions: A Complete ExplorationIn this tutorial, you'll learn the basics of working with Python's numerous built-in functions. You'll explore how to use these predefined functions to perform common tasks and operations, such as mathematical calculations, data type conversions, and string manipulations.
For additional information on related topics, take a look at the following resources:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4