Python tuple operations (tuple)

Source: Internet
Author: User

‘‘‘
A tuple is defined in the same way as a list, but the entire set of elements is surrounded by parentheses, not square brackets.
The elements of a Tuple are sorted in the order defined by the list.
The tuples index is the same as list starting from 0, so the first element of a non-empty tuple is always t[0].
A negative index is counted as a list from the tail of a tuple.
As with list shards (slice) can also be used. Note that when you split a list, you get a new list;
When a tuple is split, a new tuple is obtained.
‘‘‘
(1) You cannot add elements to a tuple. The Tuple does not have a append or extend method.
(2) You cannot delete an element from a tuple. The Tuple does not have a remove or pop method.
(3) You cannot find elements in a tuple. Tuple does not have an index method.
(4) However, you can use in to see if an element exists in a tuple.

A Tuple operates faster than a list. If you define a constant set of values, and the only
What you do with it is to constantly traverse it, using a tuple instead of a list.
? You can make your code more secure if you write-protect the data that you do not need to modify. Use
A tuple, rather than a list, has an implied Assert statement that indicates that the data is often
Amount If you have to change these values, you need to perform a tuple-to-list conversion (you need to make
With a special function).
? Tuples can be used as key in dictionary, but
It's not a list. In fact, things are more complicated than that. Dictionary key must be immutable
Of The tuple itself cannot be changed, but if you have a list of tuple
To be variable, it is unsafe to be used as a dictionary key. only strings, integers
or other dictionary-safe tuple can be used as dictionary key.
? Tuples can be used in string formatting and we'll see it very quickly.

Note:tuple to list and to Tuple
A Tuple can be converted into a list and vice versa. The built-in tuple function receives a list and returns a
A tuple that has the same element. The list function receives a tuple and returns a list. From the effect
See, a tuple freezes a list, and the list thaws a tuple.

Python tuple operations (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.