Tuple is an unchangeable list. Once a tuple is created, it cannot be changed in any way. The difference between tuple and list is that tuple and list are defined in the same way, except that the entire element set is enclosed by parentheses rather than square brackets. The tuple elements are sorted in the same order as the list elements. The index of tuples starts from 0, so the first element of a non-empty tuple is always T [0]. Negative index and list are counted from the end of tuple. Slice can also be used like list. Note: When a list is split, a new list is obtained. When a tuple is split, a new tuple is obtained. You cannot add elements to tuple if tuple does not exist. Tuple does not have the append or extend method. You cannot delete elements from tuple. Tuple does not have the remove or pop method. You cannot search for elements in tuple. Tuple does not have the index method. However, you can use in to check whether an element exists in tuple. The advantage of using tuple is that tuple is faster than list operation. If you define a constant set of values, and the only thing you want to do with it is to continuously traverse it, use tuple instead of list. If you perform write protection on data that does not need to be modified, you can Code More secure. Using tuple instead of list is like having an implicit assert statement, which indicates that the data is a constant. If you must change these values, you must convert tuple to list. Tuple and list conversion tuple can be converted to list, and vice versa. The built-in tuple function receives a list and returns a tuple with the same elements. The list function receives a tuple and returns a list. In terms of effect, tuple freezes a list, while list restores a tuple. Other tuple applications assign multiple values at a time >>> v = ('A', 'B', 'E') >>> (x, y, z) = V explanation: V is a three-element tuple, and (x, y, z) is a three-variable tuple. Assign a tuple value to another tuple, and assign each value of V to each variable in order.
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