In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the variable and immutable data types of python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the variable and immutable data types of python".
Immutable data type
The immutable data type in python is defined as: when the value of the corresponding variable of the data type changes, the corresponding memory address will also change, which is called the immutable data type, including: int (integer), string (string), tuple (tuple)
Integer type
First, let's take a look at why integers are immutable data types. Assign a value to a variable x, then change the value of this x, and output the memory address id and data type type twice.
Id (): gets the memory address of the object
Type (): returns the type of the object when there is only one parameter. Returns a class object when there are three parameters.
# assignment x = print x memory address and data type print (id (x), type (x)) # output: 8791164511056 # change x value x = print changed x memory address and data type print (id (x), type (x)) # output: 8791164511088
From the above code, we can see that the variable x is an integer, and when the value of x changes, so does the memory address of the variable. So integers are immutable data types.
String
According to the above example, let's try to change the value of the variable x into a string to see if we can draw the conclusion that the string is an immutable data type.
# assignment x = 'abcdefg'# print x memory address and data type print (id (x), type (x)) # output: 30828000 # change x value x =' higklmn'# print changed x memory address and data type print (id (x), type (x)) # output: 30828168
In the code, when the variable changes, the memory address of the variable changes from 30828000 to 30828168. So the string is an immutable data type.
Tuple
Tuples are called read-only lists, where data can be queried but not modified. If we forcibly modify the data in the tuple, we will report an error.
T = (1,2,3,4,5) # modifying the value of tuple t [1] = 100will cause an error: TypeError: 'tuple' object does not support item assignment variable data type
The definition of variable data type in python is: when the value of the corresponding variable of this data type changes, then its corresponding memory address does not change, it is called variable data type. Including: set (collection), list (list), dict (dictionary)
Set
Let's first look at the collection, define a collection set1, change the value of set1, and output the memory address twice
Set1 = {1,2,3, '123'} print ("set:", set1, "memory address:", id (set1), "\ t", "data Type:", type (set1), "\ t") # output: collection: {1,2,3,' 123'} memory address: 41388296 data Type: # add an element set1.add (41388296) print ("set:", set1) to set1 "memory address:", id (set1), "\ t", "data Type:", type (set1), "\ t") # output: collection: {1,2,3,100, '123'} memory address: 41388296 data Type:
We can see from the above code that even if a collection changes, the memory address of the collection remains the same. So the collection is a variable data type.
List list1 = [1,2,3,4,5] print ("list:", list1, "memory address:", id (list1), "\ t", "data Type:", type (list1), "\ t") # output: list: [1, 2, 3, 4, 5] memory address: 1860168 data Type: list1 [0] = 100print ("list:", list1, "memory address:", id (list1) "\ t", "data type:", type (list1), "\ t") # output: list: [100,2,3,4,5] memory address: 1860168 data type:
When the value in the list changes, the memory address remains the same, so the list is a variable data type.
Dictionaries
Dictionary is the only mapping type in python, which stores data in the form of key-value pairs (key-value). The key value in the dictionary must be a hashable data type, that is, integers, strings, tuples, etc.
Dict1 = {'name':' kkk', 'age': 18} print ("dictionary:", dict1, "memory address:", id (dict1), "\ t", "data type:", type (dict1), "\ t") # dictionary: {' name': 'kkk',' age': 18} memory address: 31033960 data type: dict1 ['sex'] =' man'print ("dictionary:", dict1 "memory address:", id (dict1), "\ t", "data Type:", type (dict1), "\ t") # Dictionary: {'name':' kkk', 'age': 18,' sex': 'man'} memory address: 31033960 data Type:
As you can see from the above code, when the elements in the dictionary change, the memory address remains the same, all 31033960. So dictionaries are variable data types.
Thank you for your reading, the above is the content of "what are the variable and immutable data types of python". After the study of this article, I believe you have a deeper understanding of what the variable and immutable data types of python are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.