Python Learning Note 5-tuples

Source: Internet
Author: User

Tuples are enclosed in parentheses, where elements are separated by commas. The element type in the tuple is any Python data.

>>> t = 123,'abc', ["come"," here " ]>>> t ('abc', ['come'  'here'])
>>> type (t)
<type ' tuple '

Indexes and slices

Because there is knowledge about lists and strings, they are sequence types, and tuples are also. Therefore, the basic operations of tuples are the same as they are.

>>>T (1,' at', [123,'ABC'], ('python','Learn'))>>> t[2][123,'ABC']>>> t[1:] (' at', [123,'ABC'], ('python','Learn'))>>> T[2][0]#Oh, yes, I can do that in list .123>>> t[3][1]'

If there is only one element in a tuple, you should add a half-width comma after the element.

>>> A = (3)>>> type (a)'int'>>>> b = (3 ,) >>> type (b)'tuple'>

All methods that can modify the list in the list are invalidated in the tuple.

The conversion can be achieved by using list () and tuple () respectively:

>>>T (1,' at', [123,'ABC'], ('python','Learn'))>>> TLS = list (t)#tuple-->list>>>tls[1,' at', [123,'ABC'], ('python','Learn')]>>> t_tuple = tuple (TLS)#List-->tuple>>>T_tuple (1,' at', [123,'ABC'], ('python','Learn'))

It is generally believed that a tuple has such characteristics and is also the scenario it uses:
tuple is faster than list operation. If you have defined a constant set of values and the only thing to do with it is to continually traverse it, use the Tup
Le instead of list.


• You can make your code more secure if you write-protect the data that you do not need to modify. Using tuple instead of list is like having an implied
Assert statement, which indicates that this data is a constant. If you have to change these values, you need to perform a tuple-to-list conversion (you need to use a
A special function).


tuples can be used as key in Dictionary (dictionary, later), but list is not. Dictionary key must be not
Variable. The tuple itself is immutable, but if you have a list of tuple, it is considered variable and used as a dictionary
Key is not safe. Only strings, integers, or other dictionary-safe tuples can be used as dictionary keys.


tuples can be used in string formatting.

Python Learning Note 5-tuples

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.