preamble
Python is different from C/C++, it has its own characteristics of the use of variables, when you first learn python, you must remember that "everything is an object, everything is a reference to an object", in fact, this feature is similar to JAVA, so you do not have to worry about the python similar to the C/C++ pointer complexity, data in python is divided into mutable data types, immutable data types, and data types are not mutable. So in python, you don't have to worry about the complexity of pointers like in C++, in python, data is categorized into mutable and immutable datatypes.
So in the process of learning python we will definitely encounter immutable data types and mutable data types. Without further ado, let's take a look at the details together!
1. Explanation of terms
All of the following is based on memory addresses.
- Immutable data type: When the value of the corresponding variable of this data type changes, then its corresponding memory address also changes, for this data type, it is called immutable data type.
- Variable data type : A data type in which the value of the corresponding variable is changed without changing its corresponding memory address is called a variable data type.
Summarize: the address changes after the immutable data type changes, and the address does not change after the variable data type changes.
2. Classification of data types
The data types in python are: integer, string, tuple, set, list, dictionary. Let's see if they are immutable or mutable datatypes with an example.
2.1 Integer
a = 1 print(id(a),type(a)) a = 2 print(id(a),type(a)) 1912499232 <class 'int'> 1912499264 <class 'int'>
We can find that when the data is changed, the memory address of the variable is changed, then integer type is immutable data type.
2.2 Strings
b = 'djx' print(id(b),type(b)) b = 'djx1996' print(id(b),type(b)) 535056476344 <class 'str'> 535056476624 <class 'str'>
We can find that when the data is changed, the memory address of the variable is changed, then the string is immutable data type.
2.3 Tuples
A tuple is known as a read-only list, meaning that the data can be queried but not modified, but we can store a list in the elements of a tuple and change the value of the list to see if the tuple is mutable or immutable.
c1 = ['1','2'] c = (1,2,c1) print(c,id(c),type(c)) c1[1] = 'djx' print(c,id(c),type(c)) result: (1, 2, ['1', '2']) 386030735432 <class 'tuple'> (1, 2, ['1', 'djx']) 386030735432 <class 'tuple'>
We can see that although the tuple data has changed, but the memory address has not changed, but we can not use this to determine that the tuple is a mutable data type. Let's go back and think about the definition of a tuple as immutable. We have changed the value of the list in the tuple, but because the list is a mutable data type, the address of the list has not changed even though we have changed the value in the list, and the value of the list's address in the tuple has not changed, so that means the tuple has not changed. We can then assume that the tuple is an immutable data type because tuples are immutable.
2.4 Collections
Sets we commonly use for de-duplication and relational operations, sets are unordered.
s = {1,'d','34','1',1} print(s,type(s),id(s)) ('djx') print(s,type(s),id(s)) result: {'d', 1, '34', '1'} <class 'set'> 870405285032 {1, '34', 'djx', '1', 'd'} <class 'set'> 870405285032
We can see that although the collection data has changed, the memory address has not changed, then the collection is a mutable data type.
2.5 Lists
List is one of the basic datatypes in python, there are datatypes similar to lists in other languages, for example, it is called array in js, it is enclosed in [], each element is separated by comma, and it can hold various datatypes inside.
list = [1,'q','qwer',True] print(list,type(list),id(list)) ('djx') print(list,type(list),id(list)) result: [1, 'q', 'qwer', True] <class 'list'> 808140621128 [1, 'q', 'qwer', True, 'djx'] <class 'list'> 808140621128
We can see that although the list data has changed, the memory address has not changed, then the list is a mutable data type.
2.6 Dictionaries
Dictionary is the only type of mapping in python that uses key-value pairs to store data. python performs a hash function on the key and determines the storage address of the value based on the result of the computation, so dictionaries are stored in an unordered way. But after version 3.6, dictionaries started to be ordered, which is a new version feature.
The key value of a dictionary can be an integer, a string, a tuple, but not a list, a collection, a dictionary.
tuple = (1) dic = {1:2} d = { tuple:1,'key2':'djx','key3':'li'} print(d,type(d),id(d)) d['key4'] = 'haha' print(d,type(d),id(d)) result: {1: 1, 'key2': 'djx', 'key3': 'li'} <class 'dict'> 256310956320 {1: 1, 'key2': 'djx', 'key3': 'li', 'key4': 'haha'} <class 'dict'> 256310956320
We can see that although the dictionary data has changed, the memory address has not changed, then the dictionary is a mutable data type.
data type | Variable/non-variable |
integers | immutable |
string (computer science) | immutable |
tuple | immutable |
listings | changeable |
set (mathematics) | changeable |
dictionaries | changeable |
summarize
Above is the entire content of this article, I hope that the content of this article on your learning or work has a certain reference learning value, if there are questions you can leave a message to exchange, thank you for my support.