Python Learning: Tuple

Source: Internet
Author: User

Another ordered list is called a tuple: a tuple. Tuple and list are very similar, but once the tuple is initialized, it cannot be modified, for example, the name of the classmate is also listed:

>>> classmates = (‘Michael‘, ‘Bob‘, ‘Tracy‘)

Now, classmates this tuple cannot be changed, and it does not have a append (), insert () method. Other methods of acquiring elements are the same as lists, and you can use them normally, classmates[0] classmates[-1] but you cannot assign them to another element.

What is the meaning of immutable tuple? Because the tuple is immutable, the code is more secure. If possible, you can use a tuple instead of a list as much as possible.

The trap of a tuple: when you define a tuple, the elements of a tuple must be determined when defined, such as:

>>> t = (1, 2)>>> t(1, 2)

If you want to define an empty tuple, you can write () :

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

However, to define a tuple with only 1 elements, if you define this:

>>> t = (1)>>> t1

is not a tuple defined, is 1 this number! This is because the brackets () can represent both tuples and parentheses in mathematical formulas, which creates ambiguity, so Python stipulates that, in this case, the calculation is done by parentheses, and the result is naturally 1 .

Therefore, only 1 elements of a tuple definition must be added with a comma , to disambiguate:

>>> t = (1,)>>> t(1,)

Python will also add a comma when displaying a tuple of only 1 elements, , lest you misunderstand the parentheses in the mathematical sense.

Finally, let's look at a "mutable" tuple:

>>> t = (' A ', ' B ', [' A ', ' B ']) >>> t[2][0] = ' X ' >>> t[2][1] = ' Y ' >>> t (' a ', ' B ', [' X ', ' Y '])  

This tuple is defined by 3 elements, respectively ‘a‘ , ‘b‘ and a list. Doesn't it mean that once a tuple is defined, it's immutable? Why did you change it later?

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

When we put the elements of the list‘A‘And‘B‘Revision changed 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. i.e. point to ' a ' , cannot be changed to point to ' B ' , pointing to a list, cannot be changed to point to another object, but the list itself is mutable!

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.

Python Learning: Tuple

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.