a.is with ==
In python, == is used to compare whether the two values are the same, and is is used to compare whether the memory addresses of the two are the same.
n1 = -55 n2 = -55 print(n1 == n2) # True print(id(n1), id(n2)) # 1976371124624 1976371124656
n3 = 'ddd' n4 = 'ddd' print(id(n3), id(n4)) # 2543122508856 2543122508856 print(n3 is n4) # True
2. Assignment
2.1 Small data pool
The value assignment operation of the two variables is, such as (n1 = 'goulonghui', n2 = 'goulonghui), there is a small data pool concept
Small data pool: int, str are within a certain range, if the two values are the same, in order to save memory, share a memory address
int range: -5 - 256
String: ①A single letter *int(20) exists in a small data pool, including 20
②All letters or numbers, or any combination of letters and numbers exist in a small data pool, and nothing else
2.2 Mutual assignment between variables
Mutual assignment between two variables in python, the two share the same memory and the same memory address. (Two base friends share an inflatable doll instance)
n3 = [1, 2, 3, [11, 22 ]] n4 = n3 print(n3, id(n3)) # [1, 2, 3, [11, 22]] 2195629156744 print(n4, id(n4)) # [1, 2, 3, [11, 22]] 2195629156744 print(id(n3[-1])) # 2195630020424 print(id(n4[-1])) # 2195630020424
3. Shallow copy
Using the copy() command to perform a copy in python is a shallow copy.
For shallow copy, the first layer creates the latest memory address, and the second layer starts to point to the same memory address, so for the second layer and deeper layers, consistency is maintained.
l1 = [1, 2, 3, 4, [11, 22, 33]] l2 = l1.copy() # print(l1, id(l1)) # [1, 2, 3, 4, [11, 22, 33]] 2679464274312 # print(l2, id(l2)) # [1, 2, 3, 4, [11, 22, 33]] 2679465137864 # print(l1[-1], id(l1[-1])) # [11, 22, 33] 1436905199432 # print(l2[-1] , id(l2[-1])) # [11, 22, 33] 1436905199432 #Example
: # l1.append(5) # print(l1) # [1, 2, 3, 4, [11, 22, 33 ], 5] # print(l2) # [1, 2, 3, 4, [11, 22, 33]] because the two memory addresses are not the same l1[4].append(66 ) print (l1) # [1 , 2, 3, 4, [11, 22, 33, 66]] print (l2) # [1, 2, 3, 4, [11, 22, 33, 66]] because both have the same memory address
Note: slices are shallow copies
# slice belongs to shallow copy li = [1, 2, 3, 4, [11, 22, 33 ]] li1 = li[:] print (li, id(li)) # [1, 2, 3, 4, [11, 22, 33]] 2523075651976 print (li1, id(li1)) # [1, 2, 3 , 4, [11, 22, 33]] 2523076515528 different memory addresses print (li[-1], id(li[-1])) # [11, 22, 33] 2523076515656 print (li1[-1], id (li1[-1])) # [11, 22, 33] 2523076515656 same memory address
4. Deep copy
Use deepcopy() under the copy module to perform deep copy in python
Each layer in a deep copy is completely independent.
# 深拷贝 from copy import deepcopy li = [1, 2, 3, [ ' gou long hui ' , ' lao gu an tou ' ]] li1 = deepcopy(li) # print(li, id(li)) # [1, 2, 3, ['gou long hui', 'lao gu an tou']] 2009288054664 # print(li1, id(li1)) # [1, 2, 3, ['gou long hui', 'lao gu an tou']] 2009288055944 memory addresses are not the same # print(li[3], id(li[3])) # ['gou long hui ', 'lao gu an tou'] 1702936217288 # print(li1[3], id(li1[3])) # ['gou long hui', 'lao gu an tou'] 1702936218696 The memory addresses are not the same
# Example # li.append(4) # print(li) # [1, 2, 3, ['gou long hui', 'lao gu an tou'], 4] # print(li1) # [1, 2, 3, ['gou long hui', 'lao gu an tou']] li[3].append( ' ni hao a ' ) print (li) # [1, 2, 3, ['gou long hui', ' lao gu an tou', 'ni hao a']] print (li1) # [1, 2, 3, ['gou long hui', 'lao gu an tou']]