Python slice() Function
Description
slice() The function implements a slice object, mainly used for passing parameters in slicing operations.
Syntax
slice syntax:
class slice(stop)
class slice(start, stop[, step])
Parameter Description:
- start -- the starting position
- stop -- the ending position
- step -- the step interval
Return Value
Returns a slice object.
Example
The following examples demonstrate how to use slice:
myslice
=
slice
(
5
)
Set a slice that extracts 5 elements
myslice
slice
(
None
,
5
,
None
)
arr
=
range
(
10
)
arr
[
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
]
arr
[
myslice
]
Extract 5 elements
[
0
,
1
,
2
,
3
,
4
]
Python built-in function