



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Today ...
Reminder
Basics
Topics
A slice is a sublist (or substring)
The slice operator [i:j] returns subsequences
a_list = [10, 20, 30, 40, 50]
a_list[0:5] [10, 20, 30, 40, 50]
a_list[1:4] [20, 30, 40]
a_list[2:3] [30]
a_list[2:2] []
a_list[:4] [10, 20, 30, 40]
a_list[2:] [30, 40, 50]
a_list[0:-1] [10, 20, 30, 40, 50]
a_list[0:200] [10, 20, 30, 40, 50]
a_list[-1:10]
a_list[:] [10, 20, 30, 40, 50]
a list[:] does a “shallow” copy of the list
An example:
list1 = [’python’, ’is’, [’fun’, ’easy’]]
list2 = list1[:]
list [’python’, ’is’, [’fun’, ’easy’]]
list [’python’, ’is’, [’fun’, ’easy’]]
list2[2][1] = ’hard’
list [’python’, ’is’, [’fun’, ’hard’]]
list [’python’, ’is’, [’fun’, ’hard’]]