The tuple tuple in Python describes _python in detail

Source: Internet
Author: User
Tags constant in python

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

Tuple the same place as the 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.

Tuple no way to 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.
You cannot find elements in tuple. Tuple There is no index method.
However, you can use in to see if an element exists in tuple.

With the benefit of 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.

The 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.

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.