python基礎學習(八)元組
阿新 • • 發佈:2018-11-01
元組的定義
元組常用操作
在元組中有哪些常用的操作呢?按照如下操作就知道元組中提供了哪些方法:
- 在ipython3中定義一個元組,例如:info = ()
- 輸入info. 按下tab鍵,ipython3就會提示元組所能使用的方法如下:
- 常用操作
- 取值和取索引:tuple[index]、tuple.index(obj)
# 1. 取值和取索引 print(info_tuple[0]) print(info_tuple.index("zhangsan"))
- 統計和計數:tuple.count(obj)、len(tuple)
# 2. 統計計數 print(info_tuple.count("
- 取值和取索引:tuple[index]、tuple.index(obj)
迴圈遍歷
info_tuple = ("zhangsan", 19, 1.55) # 使用for來遍歷元組 for info in info_tuple: print(info)
元組的應用場景
info = ("wangwu", 28) print("我叫%s,今年%d歲" % info )
列表和元組之間的轉換