Python interview frequently asked questions: The difference between mutable data types and immutable data types

WBOY
Release: 2023-05-19 16:04:06
forward
1199 people have browsed it

Python 面试高频问题:可变数据类型和不可变数据类型的区别

Python variable data types and immutable data types are a basic and important test point. To put it simply: the variables here are variable and immutable and refer to whether the address of the data will change when the variable changes!

Variable data type: If the value of a variable is changed, it is equivalent to creating a new object (that is, the address will be changed).

Variable data type: The value of the variable changes, but the address of the object does not change.

Immutable data types: tuple, string, value.

Variable data types: dictionary, list, set.

Quote

Before talking about mutable data types and immutable data types, we need to talk about the concept of references. Python variables store references to objects, which point to objects in heap memory. Objects allocated in the heap are divided into two categories, one is mutable objects, and the other is immutable objects. For example: s1="abc".

In fact, the variable s1 is a reference to the object abc. s1 points to the memory address where abc is stored. If you want to see the address value of s1, you can use the function id, which will convert the address value into decimal. Just use print(id(s1)), as shown in the figure below:

Python 面试高频问题:可变数据类型和不可变数据类型的区别

Immutable data type

Let’s take a string as an example and go directly to the code :

s1="abc"
print(id(s1))
s1="xyz"
print(id(s1))
Copy after login

Output:

140712532603136
140712532603168
Copy after login

It can be seen from the output result that changing the value of the string type variable will also change the address.

Let’s look at this example next, which is also a question that often comes up in written interviews.

#Based on the above code, write the following code:

s2=s1
print(id(s1))
print(id(s2))
Copy after login

Output:

743316570224
743316570224
Copy after login

You can see that s2=s1 actually points to the same s2 and s1 address.

Let’s continue and change the value of s2.

s2="def"
print(id(s1))
print(s1)
print(id(s2))
print(s2)
Copy after login

Output:

879864758384
xyz
879889887984
def
Copy after login

Seeing this, we can understand why changing the value of s2 does not affect the value of s1. Because s1 and s2 point to different addresses, the value of s1 has not been changed!

Variable data type

We take a list as an example:

l = [1, 2, 3]
print(id(l))
l.remove(1) # 删除元素
print(id(l))
l.append(4) # 增加元素
print(id(l))
l[1] = '8' # 修改元素
print(id(l))
Copy after login

Output:

405927907912
405927907912
405927907912
405927907912
Copy after login

You can see the add, delete, and modify operations on the list, and the address of the list There is no change, it just changes the value of the variable, but does not create a new object, and the address of the object referenced by the variable does not change.

Look at the following example, which is similar to the previous string assignment example.

l1=['a','b','c']
l2=l1
print(id(l1))
print(id(l2))
l2.append('d')
print("************")
print(id(l1))
print(l1)
print(id(l2))
print(l2)
Copy after login

Output:

838366483528
838366483528
************
838366483528
['a', 'b', 'c', 'd']
838366483528
['a', 'b', 'c', 'd']
Copy after login

The output result will not be explained here, because the addresses of l1 and l2 are the same, so they will affect each other.

Copy of list

Some students may ask, if you want to copy the list like a string and generate two lists with the same value but different addresses, how to do it? In fact, the essence of this problem is the difference between direct assignment of list (use = for direct assignment) and copy (copy is divided into shallow copy and deep copy). I will write another article to introduce the relevant knowledge of shallow copy and deep copy in detail. Please continue to pay attention.

Here we first introduce a relatively simple method to copy, using the list() constructor, the code is as follows:

l3=['x','y','z']
l4=list(l3)
print(id(l3))
print(id(l4))
l4.append('a')
print(l3)
print(l4)
Copy after login

Output:

831456302152
831480344136
['x', 'y', 'z']
['x', 'y', 'z', 'a']
Copy after login

As you can see from the results, The addresses of l3 and l4 are different, so they will not affect each other. We can also make the two lists point to different list objects by using indexes, list generation, copy(), etc., which I won’t introduce one by one here!

The above is the detailed content of Python interview frequently asked questions: The difference between mutable data types and immutable data types. For more information, please follow other related articles on the PHP Chinese website!

source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template