Someting about Lists mutation
1 ###################################2 #Mutation vs. Assignment3 4 5 ################6 #Look alike, but different7 8A = [4, 5, 6]9b = [4, 5, 6]Ten Print "Original A and B:", A, b One Print "is they same thing?"+ F isb A -A[1] = 20 - Print "New A and B:", A, b the Print - - ################ - #aliased + -c = [4, 5, 6] +D =C A Print "Original C and D:", C, D at Print "is they same thing?"+ D isD - -C[1] = 20 - Print "New C and D:", C, D - Print - in ################ - #Copied to +e = [4, 5, 6] -f =list (e) the Print "Original E and F:", E, F * Print "is they same thing?"E isF $ Panax NotoginsengE[1] = 20 - Print "New E and F:", E, F the Print + A the ################################### + #Interaction with Globals - $ $A = [4, 5, 6] - - defMutate_part (x):#Note: The A defaults to global theA[1] =x - Wuyi defassign_whole (x): theA =x - Wu defAssign_whole_global (x): - Globala AboutA =x $ -Mutate_part (100) - Printa - AAssign_whole (200) + Printa the -Assign_whole_global (300) $ PrintA
Here the assignment needs to be noted:
b = A, then A, B points to the same piece of memory, to a[1] or b[1] assignment, can change the list;
b = List (a), then A, B points to different memory.
Global vs Local
Mutate_part (x) This function changes the a[1], where a is defaulted to global;
A in Assign_whole (x) is local.
Assign_whole_global (x) wants to make changes to global variables, so add the Global keyword
Lists & Tuples (multi-group)
difference: Lists is mutable; Tuples is immutable, strings is the same as tuples, immutable.
Python Learning Note--coursera