This is the 20th day of my participation in the 18th Gwen Challenge. For details, see: the last Gwen Challenge 2021
Preface: more yesterday’s releaseStandard types in Python (changeable)This is an overview of the types that can be changed.
Basic types that can be changed:
The basic types that are immutable in Python are Dictionary, Set, List.
The Dictionary
First let’s take a look at the dictionary creation and why it is modifiable, with the following code:
A = {" name ":" the great food and want to play ", "age" : 24} a1 = dict ({" name ":" the great food and want to play ", "age" : 24}) print(a) print(type(a)) print(type(a1)) print(id(a)) print(id(a)) print(id(a)) print(id(a))Copy the code
A is created directly, and A1 is created using dict functions. They are all dictionary types. You can also see that my content has changed, but the ID of the current object remains the same, thus confirming that the dictionary is mutable.
Set
After looking at the dictionary, let’s take a look at the creation of a collection and why it is mutable, as follows:
a = {1, 2, 3, 4}
a1 = set({1, 2, 3, 4})
print(a)
print(type(a))
print(type(a1))
print(id(a))
a.remove(1)
print(a)
print(id(a))
a.add(5)
print(a)
print(id(a))
Copy the code
The above is the creation of the collection and whether mutable judgment, from the above we can compare the dictionary, so it is not difficult to see that the collection type is changeable.
List (List)
Having looked at collections and dictionaries, let’s take a look at creating lists and why they are mutable, as follows:
a = [1, 2, 3, 4]
a1 =list({1, 2, 3, 4})
print(a)
print(type(a))
print(type(a1))
print(id(a))
a.append(5)
print(a)
print(id(a))
Copy the code
Above is the list creation and mutable judgment, from the above we can compare dictionaries and collections, so it is not difficult to see that collection types can be changed.