Python Foundation 5 (tuple)

Source: Internet
Author: User

One, tuple characteristics

1, similar list, but immutable type, because of this, it can do a dictionary key
2, when working with a set of objects, this group is the tuple type by default
3, all the multi-object, comma-delimited, not explicitly defined by the symbol of these are the tuple type by default

>>>,'ethon'('ethon')> >> x,y=1,2>>> x, y (1, 2)>>> ()        #  empty tuple () >>>,       #  A value of a tuple (42,)

Attention:

1. Cannot add element to tuple, tuple does not have append () or extend () method.
2. You cannot delete an element from a tuple, and the tuple does not have a remove () or pop () method.
3. You can find an element in a tuple, because the operation does not change the tuple.
4. You can also use the In operator to check if an element exists in a tuple.

Benefits of tuples:
1. Tuples are faster than lists, and if you define a series of constant values and all you need to do is traverse it, use a tuple substitution list
2. "write protection" of data that does not need to be changed will make the code more secure
3. tuples can be used as keys in a map, and lists cannot be

Python Foundation 5 (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.