Contents
Bạn đang tìm kiếm từ khóa Why list is mutable and tuple is immutable in Python? được Cập Nhật vào lúc : 2022-01-15 19:11:18 . Với phương châm chia sẻ Kinh Nghiệm Hướng dẫn trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi Read nội dung bài viết vẫn ko hiểu thì hoàn toàn có thể lại phản hồi ở cuối bài để Tác giả lý giải và hướng dẫn lại nha.
Ventsislav Yordanov
Nội dung chính
Feb 3, 2022·5 min read
Source: ://.quora/Can-you-suggest-some-good-books-websites-for-learning-Python-for-a-layman
After reading this blog post youll know:
All the data in a Python code is represented by objects or by relations between objects. Every object has an identity, a type, and a value.
An objects identity never changes once it has been created; you may think of it as the objects address in memory. The is operator compares the identity of two objects; the id() function returns an integer representing its identity.
An objects type defines the possible values and operations (e.g. does it have a length?) that type supports. The type() function returns the type of an object. An object type is unchangeable like the identity.
The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable.
The mutability of an object is determined by its type.
Important note
Some objects contain references to other objects, these objects are called containers. Some examples of containers are a tuple, list, and dictionary. The value of an immutable container that contains a reference to a mutable object can be changed if that mutable object is changed. However, the container is still considered immutable because when we talk about the mutability of a container only the identities of the contained objects are implied.
In the next section, well see more information and detailed examples to understand more about the differences between the mutable and immutable objects.
Its time for some examples. Lets start by comparing the tuple (immutable) and list (mutable) data types. We can define a list using square brackets [] like this: numbers = [1, 2, 3]. To define a tuple, we just need to replace the brackets with parentheses () like this: numbers = (1, 2, 3). From both data types, we can access elements by index and we can iterate over them. The main difference is that a tuple cannot be changed once its defined.
Output:
1
10
Output:
[100, 2, 3]——————————————————————–
TypeError Traceback (most recent call last)
in ()
3 list_values[0] = 100
4 print(list_values)
—-> 5 set_values[0] = 100
TypeError: ‘tuple’ object does not tư vấn item assignment
We can see that when we try to change the tuple we get an error, but we dont have that problem with the list.
Now, we can try to expand our list and tuple using the += operator. This will operation work for both data types. Lets see what will happen.
Output:
2450343168136
2450343205552
2450343168136
2450341742248
We can see that the list identity is not changed, while the tuple identity is changed. This means that we have expanded our list, but created a completely new tuple. Lists are more memory efficient than tuples.
We have seen that some of the other immutable data types are integers and strings. Once they are initialized, their values cannot be changed.
Output:
1657696608
1657696640
Output:
2450343168944
2450343426208
We see that both for the number and text variables, their identity is changed. This means that new variables are created in both cases.
Lets see what happens if we give two names of the same object for a mutable data types.
Output:
2450343166664
2450343166664
True
[4, 5, 6, 7]
[4, 5, 6, 7]
We can see that the variable names have the same identity meaning that they are referencing to the same object in computer memory. Reminder: the is operator compares the identity of two objects.
So, when we have changed the values of the second variable, the values of the first one are also changed. This happens only with the mutable objects. You can see how you can prevent this in one of my previous blog posts.
Lets try to do a similar example with an immutable object. We can try to copy two strings and change the value in any of them.
Output:
3063511450488
3063511450488
True
3063551623648
3063511450488
False
Python is awesome
Python
Every time when we try to update the value of an immutable object, a new object is created instead. Thats when we have updated the first string it doesnt change the value of the second.
Sometimes we dont want to compare the identity of two objects, but to compare the values of these objects. We can do this using the == operator.
Output:
True
False
We can clearly see the two objects have the same values, but their identities are different.
As we said before the value of an immutable container that contains a reference to a mutable object can be changed if that mutable object is changed. Lets see an example of this.
Output:
(129392130, [‘Programming’, ‘Machine Learning’, ‘Statistics’])
(129392130, [‘Programming’, ‘Machine Learning’, ‘Maths’])
We have changed the value of the skills variable. The other variable person contains a reference to the skills variable and thats why its value is updated, too.
Reminder
The object is still considered immutable because when we talk about the mutability of a container only the identities of the contained objects are implied.
However, if your immutable object contains only immutable objects, we cannot change their value. Lets see an example.
Output:
1657696608
1657696032
(42, 24, (‘Python’, ‘pandas’, ‘scikit-learn’))
1657696864
1657696064
(42, 24, (‘Python’, ‘pandas’, ‘scikit-learn’))
Remember when you try to update the value of an immutable object, a new object is created instead.
You can also check my previous blog posts.
If you want to be notified when I post a new blog post you can subscribe to my newsletter.
Here is my LinkedIn profile in case you want to connect with me. Ill be happy to be connected with you.
Thank you for the read. I hope that you have enjoyed the article. If you like it, please hold the clap button and share it with your friends. Ill be happy to hear your feedback. If you have some questions, feel không lấy phí to ask them.
://.youtube/watch?v=UYcNq2_jPIs
Reply
3
0
Chia sẻ
Bạn vừa đọc tài liệu Với Một số hướng dẫn một cách rõ ràng hơn về Clip Why list is mutable and tuple is immutable in Python? tiên tiến và phát triển nhất
Bạn đang tìm một số trong những Chia SẻLink Tải Why list is mutable and tuple is immutable in Python? miễn phí.
Nếu sau khi đọc nội dung bài viết Why list is mutable and tuple is immutable in Python? vẫn chưa hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Ad lý giải và hướng dẫn lại nha
#list #mutable #tuple #immutable #Python
Tra Cứu Mã Số Thuế MST KHƯƠNG VĂN THUẤN Của Ai, Công Ty Doanh Nghiệp…
Các bạn cho mình hỏi với tự nhiên trong ĐT mình gần đây có Sim…
Thủ Thuật về Nhận định về nét trẻ trung trong môi trường tự nhiên vạn…
Thủ Thuật về dooshku là gì - Nghĩa của từ dooshku -Thủ Thuật Mới 2022…
Kinh Nghiệm Hướng dẫn Tìm 4 số hạng liên tục của một cấp số cộng…
Mẹo Hướng dẫn Em hãy cho biết thêm thêm nếu đèn huỳnh quang không còn…