Python从入门到高手(内部资源-配学习资料)_哔哩哔哩_bilibili
# ''':cvar # 元组: # 类似列表(当成容器) # 特点: # 1.定义的符号:() # 2.元组中的内容不可修改 # 3.关键字:tuple # 列表 元组 # [] () # ''' # t1 = () # print(type(t1)) # # t2 = ('hello',) # print(type(t2)) # # t3 = ('aa', 'bb') # print(type(t3)) # # import random # # list1 = [] # for i in range(10): # ran = random.randint(1, 20) # list1.append(ran) # print(list1) # t5 = tuple(list1) # print(t5) # # # 查询:下标index 切片[:] # print(t5[0]) # # print(t5[-1]) # # print(t5[2:-3]) # # print(t5[::-1]) # # # 最大值 最小值 # print(max(t5)) # print(min(t5)) # # # 求和 # print(sum(t5)) # # # 求长度 # print(len(t5)) # # 元组中的函数: # # index()---个数 # # count()---下标位置 # print(t5.count(4)) # 个数 # print(t5.index(4)) # 从t5这个元组中找出4的下标位置 # 拆包 t1 = (4, 7, 3) a, b, c = t1 print(a, b, c) a = t1 print(a) # 变量个数与元组个数不一致 t1 = (2, 5, 8, 9, 7) a, *_, c = t1 print(a, c, _) a, c, *_ = t1 print(a, c, _) a, b, *c = t1 print(a, b, c) t1 = (9,) a, *b = t1 # *b表示未知个数0~n,多个元素的化~[1,2,3,4,...] print(a, b) print(*_) # *表示拆分 print(*[4, 8, 6]) ''':cvar 字符串 x,y,*z='hello' ''' ''':cvar 系统函数: max() min() sum() len() sorted() 排序,返回的结果是列表 tuple() 元组类型的强制转换 元组自带函数: index() count() 拆装包 x,*y=(1,2,3,4,5) print(y) print(*y) ''' t2 = (4, 5) + (1, 2) print(t2) t3 = (3, 4) * 2 print(t3) print(t2 is t3) print(3 not in t3) print(len(t2)) print(tuple(sorted(t2)))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)