Python基礎---元組
阿新 • • 發佈:2018-12-12
元組 ()括號表示
tuple = ('hello',100,3.14)
區別:與列表類似,元組的元素不能修改
元組基本操作:
1.訪問
2.修改:不能修改
3.合併
4.刪除 del 只能刪除整個元組 del tuple
tuple1 = ('hello',100,3.14) print(tuple1[0]) print(tuple1[1]) print(tuple1[2]) tuple2=(1,23,456) tuple3=tuple1+tuple2 print(tuple3)
元組運算子
len(tuple) 計算元素個數 3
(1, 2, 3) + (4, 5, 6) 連線 (1, 2, 3, 4, 5, 6)
['Hi!'] * 4 複製 ['Hi!', 'Hi!', 'Hi!', 'Hi!']
3 in (1, 2, 3) 元素是否存在 True
for x in (1, 2, 3): print x 迭代遍歷 1 2 3
tuple(index1,index2) 元組索引,擷取
元組內建函式
cmp(tuple1, tuple2) 比較兩個元組元素
len(tuple) 計算元組元素個數
max(tuple) 返回元組中元素最大值
min(tuple) 返回元組中元素最小值
tuple(seq) 將列表轉換為元組
多維元組
tuple1 = [(2,3),(4,5)]
print(tuple1[0])
print(tuple1[0][0])
print(tuple1[0][1])
tuple2 = tuple1+[(3)]
print(tuple2)
print(tuple2[2])