Goal More Lists! Copy or Original? It matters! UPDATE: Intro CS Exam Friday. I will give the quizzes back Monday. The test will cover any python we have done so far. PAIR PROGRAMMING, half of you must now switch seats with someone so follow directions! Do Now ON PAPER! DO NOT LOG IN:Given: L=['a', 'b', 'c', 'd', 'e', 'f'] Evaluate each one of these: 6. L[1::2] 7. "Betty Boop"[::2] 8. "Betty Boop"[1::3] 9. "abcdefghijklmnop"[3:10:2] 10."abcdefghijklmnop"[3:11:2]Find: (when not found)When you can't find a target in a string the find method returns -1, you have to check for this manually, because of the next part.Critical Point 1:CONCATENATION MAKES A NEW COPY OF THE LIST:>>> [1] + [2] #add lists works like adding strings, it concatenates them [1, 2] >>> [1] + 2 #you cannot concatenate a list and a non-list TypeError: can only concatenate list (not "int") to list >>> x = [1,2] >>> y = [3,4] >>> print x+y #concatenation or slicing always make new copies [1, 2, 3, 4] >>> print x #so you should notice x and y don't change when you write x+y [1, 2] >>> print y [3, 4] >>> z = x + y #you can store changes by assigning them to any variable (including x and y) >>> print z [1, 2, 3, 4] Critical Point 2:SLICING MAKES A NEW COPY OF THE LIST:>>>print x[2:] [3, 4] >>>print x #x isn't changed [1, 2, 3, 4] Critical Point 3:Some List methods modify a list:>>> x =[1, 2, 6] >>> y =[3, 4] >>> x.append(6) #append puts a value into the list at the end of the list >>> print x [1, 2, 6] >>> print y [3, 4] >>> x.extend(y) #extend is similar to concatenating, but it changes the existing list >>> print x #notice that i didn't say x=something, and x changed. [1, 2, 6, 3, 4] >>> print y [3, 4] BONUS!!! This section is so you are aware of how python behaves, I don't expect you to use it regularly, nor will I explicitly test on it. It may be useful if you want to solve certain problems, but there are always alternatives. Negative Index Values: (you should recognize this and understand how to use negative indices, but you don't need to write code with it)x = 'abefg' x [ len(x) - 1] is 'g' x [ len(x) - 2] is 'f' etc... x [ len(x) - len(x)] is 'a' BUT you can index them using negative numbers the same way! (notice the same as before without len(x) ) x [ - 1] is 'g' x [ - 2] is 'f' etc... x [ - len(x)] is 'a' Remember:positive index values go from 0 to len-1. negative index values go from -1 to -len. (since you cannot use 0) Negative works with slices too! >>> x='0123456789' >>> x[::-1] #a negative step can be used! '9876543210' >>> x[-3:-1] #starting with the 7, up to but not including the last character '78' #THIS IS FYI, but not required:>>> x[-3:-1:-1] #same range as before with a negative step doesn't work! Because the indices are reversed! '' >>> x[-1:-3:-1] #you now start with the new left-most index, which is the -1. '98' PAIR PROGRAMMING, half of you must now switch seats with someone that was not in your pair, so you can work with a different person. DO NOT USE THE BUILT IN COMMANDS THAT SOLVE THE WHOLE PROBLEM! Write them yourself. (Do not use sum(L) to solve sumList, or join to solve makeSentence, etc.) PyLab06 - List methods Lets see how we can write these functions: (complete at home) 1. sumList(L) a function that takes a list of integers as a parameter, and returns the sum of the elements of the list. sumList([3,4,10]) returns 17. sumList( [ ] ) returns 0. 2. makeSentence(L) Take a list of words(strings), and join them in one string with spaces BETWEEN the words. makeSentence( ['The','rain','in','Spain','falls','mainly']) returns 'The rain in Spain falls mainly' (no space at end) makeSentence( []) returns '' (empty string) 3. makeListOfSquares(n) returns a list of the first n perfect squares. makeListOfSquares(5) returns [1,4,9,16,25] 4. makeFibList(n) takes a non-negative integer n, and returns a list of the first Fibonacci numbers up to the n'th number counting 0 as the 0th number. Do NOT use a separate fib function for this, instead use the values you put into the list to help you find more values. makeFibList(0) -> [0] makeFibList(5) -> [ 0, 1, 1, 2, 3, 5] |