Python's creation of tuple and "mutable" tuple

Source: Internet
Author: User

Create a tuple of Python

Tuple is another ordered list, and Chinese is translated as "tuple". Tuple and list are very similar, however,once a tuple is created, it cannot be modified.

The same is the name of the class, expressed as a tuple as follows:

>>> t = (' Adam ', ' Lisa ', ' Bart ')

The only difference between creating a tuple and creating a list is the ( ) substitution [ ] .

Now, this t cannot be changed, the tuple has no append () method, and there is no insert () and Pop () method. Therefore, the new classmate can not directly add to the tuple, old classmates want to quit a tuple also not.

The way to get a tuple element is exactly the same as the list, and we can access the element normally using indexed methods such as t[0],t[-1], but cannot be assigned to any other element, but it is not believed to try:

>>> t[0] = ' Paul ' Traceback (most recent):  File "<stdin>", line 1, in <module>typeerror: ' Tuple ' object does not support item assignment



Python's Create cell element tuple

Tuple and list, can contain 0, one and any number of elements.

A tuple that contains multiple elements, which we have created earlier.

A tuple of 0 elements, which is an empty tuple, is represented directly by ():

>>> t = () >>> print t ()

What about creating a tuple with 1 elements? To try:

>>> t = (1) >>> print T1

It seems to be wrong! T is not a tuple, but an integer 1. Why is it?

Because () both can represent a tuple and can be used as parentheses to indicate the priority of the operation, the result (1) is calculated by the Python interpreter as result 1, which results in not being a tuple, but an integer 1.

It is precisely because the tuple with the () definition of a single element is ambiguous, so Python specifies that the element tuple should have a comma ",", which avoids ambiguity:

>>> T = (1,) >>> print T (1,)

Python also automatically adds a "," when printing cell tuples, in order to tell you more explicitly that this is a tuple.

The multivariate tuple plus does not add this extra "," effect is the same:

>>> T = (1, 2, 3,) >>> print T (1, 2, 3)
Python's "mutable" tuple

We saw in the front that the tuple cannot be modified once it is created. Now, let's look at a "mutable" tuple:

>>> t = (' A ', ' B ', [' A ', ' B '])

Notice that T has 3 elements:' A ', ' B ' and a list:[' A ', ' B ']. List as a whole is the 3rd element of a tuple. The list object can be obtained by t[2]:

>>> L = t[2]

Then we change the list's two elements:

>>> l[0] = ' X ' >>> l[1] = ' Y '

Then look at the contents of the tuple:

>>> print T (' A ', ' B ', [' X ', ' Y '])

Doesn't it mean that once a tuple is defined, it's immutable? What's the change now?

Don't worry, let's take a look at the definition when the tuple contains 3 elements:

When we modify the list's Elements ' A ' and ' B ' to ' X ' and ' Y ', the tuple becomes:

On the surface, the elements of a tuple do change, but in fact it is not a tuple element, but a list element.

The list that the tuple initially points to is not changed to another list, so the so-called "invariant" of a tuple is that each element of a tuple is directed to never change. That point ' a ', it cannot be changed to point to ' B ', pointing to a list, cannot be changed to point to other objects, but the list itself is variable!

After understanding "point to Invariant", how do you create a tuple that does not change the content? It is important to ensure that each element of a tuple cannot be changed.

A tuple is defined:

t = (' A ', ' B ', [' A ', ' B '])

Because t contains a list element, the contents of the tuple are mutable. Can you modify the above code so that the tuple content is not mutable? Reference Answer:

t = (' A ', ' B ', (' A ', ' B '))
Print T











Python's creation of tuple and "mutable" tuple

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.