元组

tuple 元组:
( )  --在元组里,我们没办法增加、删除、修改里面的元素,只能够读取里面的元素。
a = ('hello','python','linux','mysql')
print(type(a))
print(a) --加了()就是元组类型

 

读取元组的第一个元素:
a = ('hello','python','linux','mysql')
print(a[1])
print(a)
 
查询元组里面有多少个元素:
a = ('hello','python','linux','mysql')
print(len(a))
print(a)

 

当()中只有1个元素时,又想把它当成1个元组,在元素后面加个逗号。
a = (1,)
print(type(a))

 

统计元素出现的次数:count()
a = ('a','b','c','c','c')
b = a.count('c')
print(b)   --3次

 

统计元素排序的下标:index()
a = ('a','b','c','c','c')
b = a.index('c')
print(b)    --2次

 

 

 

 
posted @ 2022-03-02 19:19  鹿鹿酱  阅读(47)  评论(0)    收藏  举报