元組
序列型別之一,不可更改,常作為引數
1.元組的創建
使用( )創建,元素用逗號隔開
例如:
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = "a", "b", "c", "d" #也可以省略小括號,但是建議不要省略
tup4=() # 建立空元組
tup5=(5,) # 含有一個元素的元組,逗號不能省略
2.元組的訪問
同串列
元組中的元素不可修改,但可以用 + (元組連接)和 * (元組復制)對元組進行組合
3.函式
|
函式 |
作用 |
例子 |
|
min() |
回傳元組中元素最小值, |
tuple1 = ('2', '5', '1') min(tuple1) 結果:1 |
|
max() |
回傳元組中元素最大值, |
tuple1 = ('2', '5', '1') max(tuple1) 結果:5 |
|
tuple() |
將可迭代系列轉換為元組, |
list1 = ['hello', 'tom', 'jack'] tuple1=tuple(list1) tuple1 結果:('hello', 'tom', 'jack') |
|
len() |
計算元組元素個數, |
tuple1 = ('hello', 'tom', 'jack') len(tuple1) 結果:3 |
|
del |
洗掉元組 |
tuple1 = ('hello', 'tom', 'jack') del tuple1 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/146938.html
標籤:Python
下一篇:用JTable 實作日歷
