# What is the difference between these two pieces of code? List1 = [1,2,3,4,5] List2 = [1,2,3,4,5] def proc (mylist): mylist=mylist+[6,7] def proc2 (mylist): Mylist.append (6
) Mylist.append (7) # Can You explain the results given by the print statements below?
Print "Demonstrating proc" Print List1 proc (list1) Print list1 print print "Demonstrating proc2" Print List2 proc2 (list2) Print List2 ' demonstrating proc [1, 2, 3, 4, 5] [1, 2, 3, 4, 5] demonstrating proc2 [1, 2, 3, 4, 5] [1, 2, 3, 4, 5, 6, 7] "# Python has a special assignment syntax: + =. Here is a example:list3 = [1,2,3,4,5] List3 = [6, 7] # does this behave like List1 = List1 + [6,7] or list2.append ([6 , 7])?
Write A # procedure, proc3 similar to proc and PROC2, but for + =. def proc3 (mylist): MyList + = [6,7] List4 = [1,2,3,4,5] proc3 (LIST4) #>>> list4 [1, 2, 3, 4, 5 , 6, 7] #注意, after calling PROC3 (LIST4), List4 is now attached with the element [6,7], as PROC2. Therefore, we find that using + + and adding values to variables (append).