A tuple of Python sequential collections

Source: Internet
Author: User

This article extracts from the MU class net "The introduction of Python"

1. Tuple characteristics

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 only difference between creating a tuple and creating a list is the ( ) substitution [ ] .

Now, this  t  cannot be changed, thetuple has no append () method, and there is no insert () and Pop () method . Therefore, the new element cannot be added directly to a tuple, nor can it be deleted by a tuple.

A tuple element is obtained in a way that is identical to the list and can be accessed normally using an indexed way such as t[0],t[-1], but cannot be assigned to another element .

>>> t[0] = ' Paul '
Traceback (most recent):
File "", Line 1, in
TypeError: ' Tuple ' object does not support item assignment

2. Create a cell element tuple

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 T
1

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.

3. "Variable" tuple

A tuple is also "mutable", such as:

>>> 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 '])

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 of Python sequential collections

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.