Python slice() Function
slice()It is a built-in function in Python used to create slice objects.
Slice objects are used to specify the slicing range of a sequence and can be used anywhere slice operations are supported, such as lists, tuples, strings, etc.
Word Definition: sliceIt means "slice" and is used to create slice objects.
Basic Syntax and Parameters
Syntax Format
slice(stop) slice(start, stop) slice(start, stop, step)
Parameter Description
- Parameter start(Optional):
- Type: Integer
- Description: The starting position of the slice, default is 0.
- Parameter stop:
- Type: Integer
- Description: The ending position of the slice (exclusive).
- Parameter step(Optional):
- Type: Integer
- Description: The step size of the slice, default is 1.
Function Description
- Return value: Returns a slice object.
- Features: Slice objects can be reused, which is very convenient.
Examples
Example 1: Creating a Slice Object
Example
# Create a slice object
s1 = slice(5) # From 0 to 5
s2 = slice(1, 5) # From 1 to 5
s3 = slice(0, 10, 2) # From 0 to 10, step 2
# Use in list slicing
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(my_list[s1]) # Output: [0, 1, 2, 3, 4]
print(my_list[s2]) # Output: [1, 2, 3, 4]
print(my_list[s3]) # Output: [0, 2, 4, 6, 8]
s1 = slice(5) # From 0 to 5
s2 = slice(1, 5) # From 1 to 5
s3 = slice(0, 10, 2) # From 0 to 10, step 2
# Use in list slicing
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(my_list[s1]) # Output: [0, 1, 2, 3, 4]
print(my_list[s2]) # Output: [1, 2, 3, 4]
print(my_list[s3]) # Output: [0, 2, 4, 6, 8]
Expected output:
[0, 1, 2, 3, 4] [1, 2, 3, 4] [0, 2, 4, 6, 8]
Code explanation:
slice(5)Equivalent to[0:5]。slice(1, 5)Equivalent to[1:5]。slice(0, 10, 2)Equivalent to[0:10:2]。
Example 2: Properties of a Slice Object
Example
# View the properties of the slice object
s = slice(1, 10, 2)
print(s.start) # Output: 1
print(s.stop) # Output: 10
print(s.step) # Output: 2
# Negative indexing
s = slice(-3, -1)
print(s.start) # Output: -3
print(s.stop) # Output: -1
s = slice(1, 10, 2)
print(s.start) # Output: 1
print(s.stop) # Output: 10
print(s.step) # Output: 2
# Negative indexing
s = slice(-3, -1)
print(s.start) # Output: -3
print(s.stop) # Output: -1
Expected output:
1 10 2 -3 -1
Code explanation:
- Slice objects have three properties: start, stop, and step.
- Negative indexing is supported.
Example 3: Practical Application
Example
# Create a reusable slice
first_three = slice(3)
last_three = slice(-3, None)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(my_list[first_three]) # Output: [1, 2, 3]
print(my_list[last_three]) # Output: [7, 8, 9]
# Used for multi-dimensional slicing
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
row = slice(0, 2) # First two rows
col = slice(1, 3) # Columns 2-3
print([row[col] for row in matrix[row]])
# Output: [[2, 3], [5, 6]]
first_three = slice(3)
last_three = slice(-3, None)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(my_list[first_three]) # Output: [1, 2, 3]
print(my_list[last_three]) # Output: [7, 8, 9]
# Used for multi-dimensional slicing
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
row = slice(0, 2) # First two rows
col = slice(1, 3) # Columns 2-3
print([row[col] for row in matrix[row]])
# Output: [[2, 3], [5, 6]]
Expected output:
[1, 2, 3] [7, 8, 9] [[2, 3], [5, 6]]
The main advantage of slice objects is that they can be reused, making code clearer.
Other Extensions
Python3 Built-in Functions