Python-元祖类型(tlupe)

#元祖就是"一个不可变的列表"
1. 作用: 按照索引/位置存放多个值,只能用于读们不能用于改
2. 定义:()内用逗号分隔开多个任意类型的元素
# t=(11,22,"llj")  #t=tlple(11,22,"llj")
# print(t,type(t))
# (11, 22, 'llj') <class 'tuple'>

# x=(10) #单独一个括号代表包含的意思
# print(x,type(x))
# 10 <class 'int'>

# t=(10,) #如果元祖只有一个元素,必须加逗号
# print(t,type(t))
# (10,) <class 'tuple'>

t=(1,2,"aa") #t=(0号索引-->"值1的内存地址",1号索引-->"值2的内存地址",3号索引-->"值3的内存地址")

3. 类型转换
# res_tuple=tuple("llj")
# print(res_tuple)
# ('l', 'l', 'j')

# res_tuple=tuple([1,22,"llj"])
# print(res_tuple)
# (1, 22, 'llj')

# res_tuple=tuple({"k1":11,"k2":"llj"})
# print(res_tuple)
# ('k1', 'k2')

4. 内置方法

#按索引取值(正向+反向),只能取
t=('11','22','haha','llj')
# print(t[0]) #正向取
# ll
# print(t[-1]) #反向取
# llj

#切片
# print(t[0:2])
# ('11', '22')
# print(t[::-1])
# ('llj', 'haha', '22', '11')

#成员运算in not in
# print('11' in t)
# True
# print('11' not in t)
# False

#for循环
# for i in t:
# print(i)
# 11
# 22
# haha
# llj

# res_int=t.index("11") #查找索引
# print(res_int)
# 0

# res_int=t.count("22")  #出现的次数
# print(res_int)
# 1

 
 
posted @ 2020-07-23 10:40  梁博客  阅读(246)  评论(0)    收藏  举报