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