1. 程式人生 > >python 元組

python 元組

python

元組

>>> a = () #創建空元組

>>> print(a)

()

>>> b = (1,)

>>> print(b)

(1,)

>>> c = (1, 2, 4, 9, 20, 99) #創建元組

>>> d = (199, 888)

>>> e = (c + d) #元組拼接

>>> e

(1, 2, 4, 9, 20, 99, 199, 888)

>>> len(e) #計算元組元素個數

8

>>> max(e) #返回元組中元素最大值

888

>>> min(e) #返回元組中元素最小值

1

>>> e[2] #元組索引

4

>>> e[-2]

199

>>> e[1:] #元組截取

(2, 4, 9, 20, 99, 199, 888)

>>> f = [‘tianshi‘,‘buding‘,‘chun‘,‘xia‘]

>>> g = tuple(f) #將列表轉換為元組

>>> g

(‘tianshi‘, ‘buding‘, ‘chun‘, ‘xia‘)

>>> del g #刪除元組

>>> g

Traceback (most recent call last):

File "<pyshell#17>", line 1, in <module>

g

NameError: name ‘g‘ is not defined


本文出自 “每天進步一點點” 博客,請務必保留此出處http://zuoshou.blog.51cto.com/2579903/1978088

python 元組