This article uses Python3 as an example, including six data types:
1、 Number:
int integer
long
float
complex
Boolean (0 and 1)
2、 String
3、 List
4、 Tuple
5、 Dictionary
6、 Set
Use the id function to view the memory address of the variable i for verification (use hex(id(i)) to view the hexadecimal memory address)
Immutable types: Number, String, Tuple
Re-assigning an immutable type variable is actually re-creating an immutable type object and re-pointing the original variable to the newly created object (if no other variables refer to the original object (that is, the reference count is 0), The original object will be recycled).
For the immutable type int, no matter how many immutable types are created, as long as the values are the same, they all point to the same memory address.
For example Number:
number1=123id(number1)8790928028112
number2=123id(number2)8790928028112
number3=321id(number3)49143760
It can be seen that when i += 1 is executed, the memory address will change because the int type is immutable.
i=5id(i)8791205700368
i+=1id(i)8791205700400
j=i
id(j)8791205700400
j
6
Variable types: List, Dictionary, Set
The reassignment of the variable data type does not create a new object.
For example List:
list1=[1,2,3,4,5]id(list1)49307976
list2=[1,2,3,4,5]id(list2)49307784
list1.append(123)
list1
[1,2,3,4,5,123] id(list1)49307976
If you execute list1=list2, because list1 and list2 point to the same memory address, and the types of list1 and list2 are both List, and the variable type, modifying any one of list1 and list2 will affect the value of the other List.
list1
[1,2,3,4,5,123] id(list1)49307976
list1=list2
id(list1)49307784id(list2)49307784
list2
[1,2,3,4,5]
list2.append(456)
list1
[1,2,3,4,5,456]
list2
[1,2,3,4,5,456]
all in all:
The immutable type refers to the value (Value), the value does not change, the object does not change; the value changes, the object changes.
The variable data type refers to the name (Name), the name does not change, the object does not change; the name changes, the object changes.
Content expansion:
**Variable and immutable data in Python? **
So far, this article about whether numbers in python are variable types is introduced here. For more related content, are numbers in python immutable types, please search for ZaLou.Cn's previous articles or continue to browse related articles below. Hope everyone Support ZaLou.Cn a lot in the future!
Recommended Posts