A summary of the usage of the tuple (tuple) in Python

Source: Internet
Author: User
Tags in python

One, tuple is also a class, is immutable list type, can not be deleted.

Create:

Tup1 = (' Physics ', ' Chemistry ', 1997, 2000);
Tup2 = (1, 2, 3, 4, 5);
Tup3 = "A", "B", "C", "D";

visit: (same as List) Tup1[1:5];

modification: Can not be modified, can only add new parts;

Tup3 = Tup1 + tup2;
Print Tup3;

Second, any unsigned objects, separated by commas, default to tuples, as follows:

a=1,2,3, ' e '

a= (1,2,3, ' e ').


The Python tuple contains the following built-in functions (functions similar to the list)
1, CMP (Tuple1, Tuple2): compare two tuple elements.
2, Len (tuple): Calculate the number of tuple elements.
3, Max (tuple): Returns the maximum value of an element in a tuple.
4, min (tuple): Returns the element minimum value in a tuple.
5, tuple (seq): Converts a list to a tuple.


four, the tuple method:

1, COUNT (): Find the number of times an element appears in tuple.

2.index (): Finds the first index value of an element.


Five, Tuple is the immutable list. Once you create a tuple, you can't change it in any way.

the similarities between ①, Tuple and list

Defining tuple is the same way as defining a list, except that the entire set of elements is surrounded by parentheses rather than square brackets.
The Tuple elements are sorted in the order defined by the list. The tuples index is the same as the list starting at 0, so the first element of a non-empty tuple is always t[0].
The negative index counts as the list from the tail of the tuple.
The same fragment (slice) as the list can also be used. Note that when you split a list, you get a new list, and when you split a tuple, you get a new tuple.

methods of ② and Tuple do not exist

You cannot add elements to tuple. Tuple There is no append or Extend method.
You cannot delete an element from tuple. Tuple does not have a remove or pop method.
However, you can use in to see if an element exists in tuple.

③, the benefits of using Tuple

Tuple is faster than list operation. If you define a constant set of values, and the only thing to do with it is to iterate over it, use tuple instead of the list.
You can make your code more secure if you write-protect data that you do not need to modify. Using tuple instead of a list is like having an implied assert statement that the data is constant. If you have to change these values, you need to perform a tuple to list conversion.

conversion of ④, Tuple and list

Tuple can be converted to a list and vice versa. The built-in tuple function receives a list and returns a tuple with the same element. The list function receives a tuple and returns a list. In effect, tuple freezes a list, and the list thaws a 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.