'''lecture_17_copying_sublists.py Explore issues involved in copying sublists Oliver W. Layton CS 151: Computational Thinking: Visual Media Spring 2021 ''' # 1) Explore copying middle row then changing an item in the copy digits = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # middleRow = digits[1] # this is a list # print(digits) # print(middleRow) # middleRow[0] = -99 # print(digits) # print(middleRow) # 2) Explore copying 1st two rows then modifying the copy digits = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # firstTwo = digits[:2] # print(firstTwo) # firstTwo[0][1] = -99 # print(firstTwo) # print(digits) # 3) Access single int, then change it digits = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # theFour = digits[1][0] # int NOT an orginal sublist. So it is independent. (doesnt violate the rule) # print(theFour) # theFour = -99 # print(theFour) # print(digits) # 4) Access a list of basic types, but not one of the original sublists # e.g. the 1st two ints in the first sublist digits = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] slice = digits[1][:2] # list of the 4 and 5 print(slice) slice[0] = -99 print(slice) print(digits) print(slice) # Full slice of sublist # digits = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # slice = digits[1][:] # middleRow = digits[1] # list of the 4, 5, 6 # print('middle row:', middleRow) # slice = middleRow[:] # list of the 4, 5, 6 # print('full slice of middle row:', slice) # slice[0] = -99 # print('middle row (after change):', middleRow) # print('full slice of middle row (after change):', slice) # 5) Access a list with a sublist of 3 turtle objects and another with 1 turtle object. # Slice the 1st two ints in the first sublist import turtle myPets = [[turtle.Turtle(), turtle.Turtle(), turtle.Turtle()], [turtle.Turtle()]] # myPets has 3 turtle objects in 1st sublist. 1 turtle in 2nd sublist. print(myPets) # Slice the 1st two Turtles in the first sublist firstTwoTurts = myPets[0][:2] print('First two turtles in 1st sublist:') print(firstTwoTurts) turt = firstTwoTurts[0] # would change turtle 1st item in firstTwoTurts AND also 1st turtle in 1st sublist of myPets turt.color('red')