[Note 9] 2020Python practice Python Grammar [6] _ variable immutable type

I. Reviewing the Old:

1 storage principle, variable memory stack area and heap area

Stack area : storage is a correspondence between the variable name and memory address, it can be simply understood as: variable name stored memory address .

Heap : it is stored in the variable value .

Stressed : just stand variable name angles talk about one thing:
            assign a variable name (x = y), as well as mass participation variable name (print (x)), transfer of all data stack area
            and stack the data is correspondence between the variable name and memory address, or is the value of reference.

            Therefore, we say, python is passed by reference.

 

Second, the soft type and hard type

 1. What is the type of variable and immutable type?

Variable Type : value is changed, id unchanged , to prove to change the original value , to prove the original value can be changed.

There is spineless guy inside and out, it can not be changed.

Immutable type : value change, id has changed , proved to generate new values, without fundamentally changing the original , the original value is not proven to be modified.

Be spineless guy, although the appearance of the same, but the heart becomes ......

 

(Some foggy Ha, that's take a look at the following chestnuts, right -)

2, how to determine the type of the variable and immutable type?

2.1 int is immutable 

>>> 
>>> X =. 11 
>>> Print (ID (X))
 140,721,476,655,072 
>>> X = 10 
>>> Print (ID (X)) # just assigned, will produce a new value 
140721476655040 
>>> 
X = 10 >>>
>>> Print (ID (X))
140,721,476,655,040
>>> = X + 2
>>> Print (ID (X))
140,721,476,655,104
>>>

2.2 float is immutable

>>> 
>>> x=3.1
>>> print(id(x))
2133614198672
>>> x=3.3
>>> print(id(x))
2133614198576
>>> 

2.3 str is immutable 

>>> 
>>> x='abcd'
>>> print(id(x))
2133617073904
>>> x='qqqq'
>>> print(id(x))
2133617423536
>>> 

Summary: int, float, str are designed to become an integral whole, can not be changed. So are "immutable."

2.4 list is a variable type 

>>> 
>>> m = [ ' AAA ' , ' BBB ' , ' CCC ' ]
 >>> Print (ID (m))
 2133617504704 
>>> m [0] = ' VVV ' 
>>> Print (m) 
[ ' VVV ' , ' BBB ' , ' CCC ' ]
 >>> Print (ID (m))
 2133617504704 
>>> # list is full all of the elements, an element to change the internal value, id is the list unchanged (no move house )

2.5 dict variable type

>>> dic={'k1':111,'k2':333}
>>> print(dic)
{'k1': 111, 'k2': 333}
>>> print(id(dic))
2133617496768
>>> dic['k1']=444444
>>> print(dic)
{'k1': 444444, 'k2': 333}
>>> print(id(dic))
2133617496768
>>> # Dic and list, is a variable type (variable value, id unchanged) - the vessel or container, but the container where things can change, is the "variable type" 
>>>
separated by commas within the {} multi-open Key: value,
DIC {Key: value}, Key must be immutable type, value may be any type
>>> >>> DIC = {[l, 2,3]: 123, [1,2]: 12 is }
Traceback (MOST Recent Last Call): File
"<pyshell # 40>", Line. 1,in<Module1> DIC= {[l, 2,3]: 123, [1,2]: 12 is} TypeError: unhashable type:'List'
>>> DIC = {{ ' A ' :}. 1: 123 }
Traceback (MOST Recent Last Call): File
"<pyshell # 41 is>", line 1, in <Module1> DIC = {{ ' A ' :}. 1: 123 } TypeError: unhashable type: ' dict '
>>> # variable type, that is not the hash (unhashable type), immutable, is hashable (Hashable of the type ) >>>

2.6 bool immutable type

0--true

1--false

(Sense Ha ......)

 

 

Guess you like

Origin www.cnblogs.com/bigorangecc/p/12424896.html