



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
These lecture notes from cpsc 121 fall 2012 cover an exam overview, including topics such as fruitful functions, input functions, type conversion functions, lists, string and list operations, for loops, range function, and slices. The notes also include a warm-up exercise and examples on list slices.
Typology: Study notes
1 / 5
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’]]