Tuples in Python

Source: Internet
Author: User

One, tuple (tuple)

Tuples are basically like a list that can't be changed . supports arbitrary types of elements like lists, supports nesting, and common sequence operations.

The meta uncorrupted set is written in parentheses.

>>> info = (' Forest ', ' man ', 1991,7,13,true)    #支持不同类型 >>> info = (' Forest ', ' Man ', (1991,7,13), True)    # Support Nesting >>> info[0]    #支持常见的序列操作 ' inter-Forest ' >>> Info[:2]    #切片 (' Forest ', ' man ') >>> info[1] = ' Women ' # Immutable, sorting or re-assigning tuples is not possible traceback (most recent call last):  File "<pyshell#12>", line 1, in <module>    INFO[1] = ' Women ' TypeError: ' Tuple ' object does not support item assignment

  

  Create a tuple , the most important is not parentheses but commas

>>> tuple1 = (1)    #单单只有圆括号不能创建一个元组 >>> type (tuple1) <class ' int ' >>>> tuple2 = 1,2,3< c1/> #加上逗号就能成功创建元组, parentheses are not required >>> type (tuple2) <class ' tuple ' >>>> tuple3 = 1,    #创建单个元素的元组 > >> type (tuple3) <class ' tuple ' >>>> tuple4 = ()    #创建空元组 >>> type (tuple4) <class ' Tuple ' >

  

  Updates a tuple.

The tuple is immutable, so it is only possible to update tuples by slicing, inserting, and overwriting variable names.

>>> info = (' Forest ', ' Man ', (1991,7,13), True) >>> info = info[:2] + (173,) + info[2:] #实际上第一行中的元组并没有消失, just no variables The name points to it >>> info (' Forest ', ' man ', 173, (1991, 7, +), True)

  

  Remove the tuple.

  Delete an entire tuple from Del

>>> info (' Forest ', ' man ', 173, (1991, 7, +), True) >>> del info>>> infotraceback (most recent call Last):  File "<pyshell#36>", line 1, in <module>    infonameerror:name ' info ' are not defined

Tuples in Python

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.