This is the 19th day of my participation in the 18th Gwen Challenge. For details, see: The last Gwen Challenge 2021

Preface: I have also studied Python by myself these two days. Today, I have sorted out the knowledge points of various standard types in Phton.

It is classified in Python’s basic types, immutable and mutable

Classification of types:

An immutable base type

The basic types that are immutable in Python are Number, String, and Tuple.

Number (Number)

Python includes integers (int), floats (bool), and boons (bool). Let’s look at the code and their immutable code as follows:

Print (a,type(a)) b = 1.1 print(b,type(b)) c = True print(c,type(c)) c = True print(c,type(c) print(id(a))Copy the code

What we can get is this

From 2463640518896 and 2463640518928 below, we can see that although we print both a object, but we get their IDS are different.

The storage of object A is shown above: We can see that they’re both a’s but they have a different id, so int is changing, but it’s creating a new location in memory to store the new A’s instead of changing the original space, so the numeric type is immutable, float, bool

String (String)

Having said that, let’s talk about string creation and its immutable code as follows:

Print (id(a)) print(id(a)) print(id(a)) print(id(a))Copy the code

We can see from the above code and print that String is immutable.

Tuples (a Tuple)

Let’s talk about the creation of a meta-ancestor and its immutable code as follows:

"' in the form of a" '# directly create a = (' a', 'b', 'c') print (" a type ", type (a)) # using the built-in function b = a tuple ((' a ', 'b', 'c')) print (" b type ", type (b) a single tuples c # = (' a ') print (" c type ", type (c)), ' 'form two' ' '# directly create a1 =' a ', 'b', 'c' print (a1 type: "", type (a1) c1 = 'a', Print (c1 type: "", type (c1)) print (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ') ' ' 'judgment immutability' 'z = (' food and want to play ', '@', 'XXX') print (yz, type (yz), id (yz)) yz [1] = ' 'print (yz, type (yz), id (yz))Copy the code

From the error above we can see that the contents of the meta-ancestor are immutable.

Conclusion: We can see that the Python immutable characteristics of his, he is to create a the same as the original object, so as to bring a certain amount of overhead, especially when we write code in a function body we want to select a proper type for our action class, which can process large data operation, we can reasonable planning memory overhead.