元组
tuple = (item1, item2, ...)
定义一个空元组:
- em_tuple = tuple()
- em_tuple = ()
定义单一元素元组
- oneitem_tuple = (item, ) # 注意: 必须要加逗号!!!
元组的特点
- 一旦定义好, 就不能添加修改和删除其中的元素, 只能查看
- 例外: 如果元组中的某个元素是一个容器(比如list), 只要这个容器支持增删改, 那么该容器中的元素就能够做相应的操作
元组的查询
元组和列表的查询类似, 都没有find函数, 原因是find函数是字符串类型独占的方法!
- 查找某个元素的下标
- tuple.index(元素值)
- 统计元素出现的次数
- tuple.count(元素值)
- 元素是否存在
- item in/not in tuple