Python Interview Questions
Python
Web DevelopmentFrontendBackendData ScienceQuestion 10
Explain how slicing works in Python.
Answer:
Slicing in Python is a powerful feature that allows you to extract a portion of a sequence, such as a list, tuple, or string. It provides a way to create sub-sequences from the original sequence using a concise syntax. Here's a detailed explanation of how slicing works, including the syntax and various use cases:
Basic Syntax
The basic syntax for slicing is:
sequence[start:stop:step]
start
: The index at which the slice starts (inclusive). If omitted, the default is the beginning of the sequence.stop
: The index at which the slice ends (exclusive). If omitted, the default is the end of the sequence.step
: The step size or stride between elements. If omitted, the default is 1.
Examples
Slicing a List
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# Slicing from index 2 to 5 (exclusive)
print(my_list[2:5]) # Output: [2, 3, 4]
# Slicing from the beginning to index 5 (exclusive)
print(my_list[:5]) # Output: [0, 1, 2, 3, 4]
# Slicing from index 5 to the end
print(my_list[5:]) # Output: [5, 6, 7, 8, 9]
# Slicing the entire list
print(my_list[:]) # Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# Slicing with a step of 2
print(my_list[::2]) # Output: [0, 2, 4, 6, 8]
# Slicing with a negative step (reversing the list)
print(my_list[::-1]) # Output: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Slicing a String
my_string = "Hello, World!"
# Slicing from index 7 to 12 (exclusive)
print(my_string[7:12]) # Output: "World"
# Slicing from the beginning to index 5 (exclusive)
print(my_string[:5]) # Output: "Hello"
# Slicing from index 7 to the end
print(my_string[7:]) # Output: "World!"
# Slicing the entire string
print(my_string[:]) # Output: "Hello, World!"
# Slicing with a step of 2
print(my_string[::2]) # Output: "Hlo ol!"
# Slicing with a negative step (reversing the string)
print(my_string[::-1]) # Output: "!dlroW ,olleH"
Advanced Slicing
Omitting Parameters
- Omitting
start
: Defaults to the beginning of the sequence. - Omitting
stop
: Defaults to the end of the sequence. - Omitting
step
: Defaults to 1.
# Equivalent to my_list[0:5:1]
print(my_list[:5])
# Equivalent to my_list[5:10:1]
print(my_list[5:])
Negative Indices
You can use negative indices to slice from the end of the sequence.
# Slicing the last three elements
print(my_list[-3:]) # Output: [7, 8, 9]
# Slicing the sequence except the last three elements
print(my_list[:-3]) # Output: [0, 1, 2, 3, 4, 5, 6]
Negative Step
A negative step
value reverses the direction of the slice.
# Reversing the sequence
print(my_list[::-1]) # Output: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
# Slicing from index 7 to 2 in reverse order
print(my_list[7:2:-1]) # Output: [7, 6, 5, 4, 3]
Practical Use Cases
Copying a Sequence
You can create a shallow copy of a sequence using slicing.
copy_list = my_list[:]
print(copy_list) # Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Selecting a Subset
Extract a specific subset of a sequence.
subset = my_list[3:7]
print(subset) # Output: [3, 4, 5, 6]
Skipping Elements
Select every second element (or nth element) of a sequence.
skipped_elements = my_list[::2]
print(skipped_elements) # Output: [0, 2, 4, 6, 8]
Reversing a Sequence
Reverse the order of elements in a sequence.
reversed_list = my_list[::-1]
print(reversed_list) # Output: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Summary
Slicing is a versatile and powerful tool in Python for extracting parts of sequences like lists, tuples, and strings. Understanding how to use the start
, stop
, and step
parameters effectively allows you to manipulate and access data efficiently in your Python programs.