Thủ Thuật về Why list is mutable and tuple is immutable in Python? Chi Tiết

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.

Python Basics: Mutable vs Immutable Objects

Ventsislav Yordanov

Nội dung chính

    Python Basics: Mutable vs Immutable ObjectsIntroduction (Objects, Values, and Types)Mutable and Immutable Data Types in PythonIndexing lists and tuplesChanging values: lists vs tuplesTuple vs List ExpandingOther Immutable Data Type ExamplesCopying Mutable Objects by ReferenceCopying Immutable ObjectsThe == operatorImmutable Object Changing Its ValueOther Blog Posts by MeFinal WordsVideo liên quan

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:

    What are an objects identity, type, and valueWhat are mutable and immutable objects

Introduction (Objects, Values, and Types)

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.

Identity

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.

Type

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.

Value

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.

Mutable and Immutable Data Types in Python

    Some of the mutable data types in Python are list, dictionary, set and user-defined classes.On the other hand, some of the immutable data types are int, float, decimal, bool, string, tuple, and range.

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.

Indexing lists and tuples

Output:

1
10

Changing values: lists vs tuples

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.

Tuple vs List Expanding

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.

Other Immutable Data Type Examples

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.

Copying Mutable Objects by Reference

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.

Copying Immutable Objects

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.

The == operator

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.

Immutable Object Changing Its Value

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.

Summary

    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.An objects type defines the possible values and operations.Objects whose value can change are said to be mutable. Objects whose value is unchangeable once they are created are called immutable.When we talk about the mutability of a container only the identities of the contained objects are implied.

Resources

    ://.pythonforthelab/blog/mutable-and-immutable-objects/://standupdev/wiki/doku.php?id=python_tuples_are_immutable_but_may_changehttps://.geeksforgeeks.org/mutable-vs-immutable-objects-in-python/://docs.python.org/3/reference/datamodel.htmlhttps://docs.python.org/3/library/functions.html#id

Other Blog Posts by Me

You can also check my previous blog posts.

    Jupyter Notebook ShortcutsPython Basics for Data SciencePython Basics: Iteration, Iterables, Iterators, and LoopingPython Basics: List ComprehensionsData Science with Python: Intro to Data Visualization with MatplotlibData Science with Python: Intro to Loading, Subsetting, and Filtering Data with pandasIntroduction to Natural Language Processing for Text

Newsletter

If you want to be notified when I post a new blog post you can subscribe to my newsletter.

LinkedIn

Here is my LinkedIn profile in case you want to connect with me. Ill be happy to be connected with you.

Final Words

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ẻ

4346

Clip Why list is mutable and tuple is immutable in Python? ?

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

Chia Sẻ Link Cập nhật Why list is mutable and tuple is immutable in Python? miễn phí

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í.

Thảo Luận vướng mắc về Why list is mutable and tuple is immutable in Python?

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