Remember
A slice describes start, stop and step for sequence selection. Stop is excluded; omitted bounds use the sequence’s defaults.
A slice describes start, stop and step for sequence selection. Stop is excluded; omitted bounds use the sequence’s defaults.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'tea'
part = slice(0, 2)
print(text[part])Top-level scope
'tea'Object 2 · sliceslice(0, 2, None)A slice describes start, stop and step for sequence selection. Stop is excluded; omitted bounds use the sequence’s defaults.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'tea'
part = slice(0, 2)
print(text[part])
|
v
DISPLAYED RESULT
te
FINAL BINDINGS
text --> object 1 (str)
part --> object 2 (slice)
object 1: 'tea'
object 2: slice(0, 2, None)Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'tea'
part = slice(0, 2)
print(text[part])Top-level scope
'tea'Object 2 · sliceslice(0, 2, None)The example has finished. Compare the final output with the values and references above.
te
What do you expect this program to print?
text = 'tea'
part = slice(0, 2)
print(text[part])slice() FunctionThe slice() function in Python creates a slice object that can be used to extract parts of a sequence like a list, tuple, or string. This is very useful when you want to get a sub-part of a sequence using custom start, stop, and step values.
slice(stop)
slice(start, stop[, step])
start – (Optional) The starting index of the slice. Default is 0.stop – The ending index (exclusive).step – (Optional) The step size or interval. Default is 1.slice object that can be used to extract portions from a sequence.numbers = [10, 20, 30, 40, 50, 60]
s = slice(1, 4)
print(numbers[s])
[20, 30, 40]
letters = ['a', 'b', 'c', 'd', 'e', 'f']
s = slice(1, 6, 2)
print(letters[s])
['b', 'd', 'f']
text = "HelloWorld"
s = slice(0, 5)
print(text[s])
Hello
Python allows slicing using both slice() and the shorthand notation [start:stop:step]. They do the same thing but slice() is more flexible when you want to pass slicing as a parameter to functions or reuse slicing logic.
def get_middle_part(seq):
s = slice(1, -1)
return seq[s]
print(get_middle_part("Python"))
print(get_middle_part([10, 20, 30, 40]))
ytho
[20, 30]
stop index is exclusive.slice().slice() extracts the values itself — it only creates a slice object.Use slice() when writing reusable logic or custom sequence functions. Knowing how to slice efficiently is helpful in coding rounds involving arrays or strings.
slice() returns a slice object for use with sequences.start, stop, and step.Write a function that takes a list and returns every third element starting from index 0 using slice().
def every_third_element(lst):
s = slice(0, len(lst), 3)
return lst[s]
print(every_third_element([1, 2, 3, 4, 5, 6, 7, 8, 9]))
[1, 4, 7]
Connect what you noticed in the example with the idea below.
A slice describes start, stop and step for sequence selection. Stop is excluded; omitted bounds use the sequence’s defaults.
The stop index is included in a slice.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.