Small data pool
== determines two values are equal
It is - is determined on both sides of the same memory address
a=10 b=10 print(a is b)
id () - View the memory address
Block
Py a file, a function, a module, each row is the terminal block
int,str,bool
int: -5 ~ + infinity
a=1000 b=1000 print(id(a),id(b)) print(a,b)
str:
- Defined string can be arbitrary,
- Character string (letters, numbers) for multiplying the length of the string can not exceed 20
- Python3.7 above version is <= 4096
- You can only take the 1:00 special symbols (Chinese symbol) to multiply or multiply 0
bool
- True
- False
Case of a small data pool with the same code block in the first block of code
Resident mechanism: save memory space, improve efficiency (reduce the destruction of open space and space-consuming)
Small data pool
- int,str,bool
- int:-5~256
- str:
- Supports only letters, numbers; any length compliance mechanism residing
- When multiplying the total length of the string can not exceed 20
- Special symbols can only multiply when the multiplication 0
Copy depth
- After avoided
- The interview will ask
Assignment
a=[1,2,3,4] b=a print(id(a),id(b))
Shallow copy
Assignment: a plurality of variable names point to the same memory address
copy - copy
(Case is open, the same Nang)
a=[1,2,3,4,5] b=a.copy() print(id(a[0])) print(id(b[0]))
a=[1,2,3,4,5] b=a[:] #切片 浅拷贝 print(id(a[-1][0])) print(id(b[-1])[0])
a=[1,2,3,[4,5]] b=a[:]
Shallow copy
- Copy only of the first layer element address, modifying the first layer is modified only when the source data is not changed
- When added to the variable data source data types affected
- Variable data types can be modified to add, modify only immutable type
Deep copy
import copy - the copy module introduced
import copy a=[1,2,3,[4,5],6] b=copy.deepcopy(a)
Deep copy: common immutable data types, data type of the variable to open a new space (either how to change one of the other does not change)
set
- One data type Python, disorder, variable, natural deduplication
- Collection - set
s=set()
: Empty set- Dictionary is a collection of no value (element unique, immutable)
Collection method
increase
- s.add()
- s, update ( 'alex) - recursive addition
- print (set ( 'alex')) - recursive addition
delete
- s.remove () - deleted by elements
- s.clear () - Clear
- s.pop () - random deletion (minimum)
change
- After the first cut plus
check
- for loop
Other operations
- Subtraction: Difference Sets
- &: Intersection
- | (Pipe symbol): union
- ^: Anti intersection
>:判断是否子集
print(frozenset({1,2,3,4}))
Freeze collection (the collection of immutable)