#-----------------------元组---------------------- ''' 一、元组的特性: 1.元组不可修改 2.元组内容可以重复 3.元组只有一个元素的时候需要加逗号 4.通过索引取值 ''' #实例:元组定义 test_tuple=(1,1) print(type(test_tuple)) ''' 二、元组的操作: 2.1:索引取值 2.2:切片(正序切片,倒序切片,默认步长为1,正序切片用正数,倒序切片用负数) 2.3:元组的运算 2.4:常用方法 ''' #实例1:索引取值 test_tuple=(1,1,2,'hello',3,'a') print(test_tuple[1]) #实例2:正序切片 test_tuple=(1,1,2,'hello',3,'a') print(test_tuple[::1]) #实例3:倒序切片 test_tuple=(1,1,2,'hello',3,'a') print(test_tuple[::-1]) #实例4:切片取值 test_tuple=(1,1,2,'hello',3,'a') print(test_tuple[1:3]) #实例4:元组的运算 test_tuple01=(1,1,2,'hello',3,'a') test_tuple02=(1,1,2,'hello',3,'a') test_tuple03=test_tuple01+test_tuple02 print(test_tuple03) #-----------2.4:常用方法---------- # 2.4.1:去重方法:set #实例1:set方法去掉元组重复元素 test_tuple02=(1,1,2,'hello',3,'a') print("去掉元组重复元素",set(test_tuple02)) #实例1:count统计元组元素个数 #语法:tuple.count('value') 元组需要统计的元素 test_tuple02=(1,1,2,'hello',3,'a') test_tuple04=test_tuple02.count('a') print("统计元组元素个数为:",test_tuple04) #实例1:len查看元组长度 test_tuple02=(1,1,2,'hello',3,'a') print("元组长度为",len(test_tuple02)) #实例1:查看元组最大数 test_tuple02=(1,1,2,3) print("元组最大数为:",max(test_tuple02)) #实例1:查看元组最小数 test_tuple02=(1,1,2,3) print("元组最小数为:",min(test_tuple02)) ''' -----2.4.2:元组的分类-------- 1.不可变元组:元组里面的元素可变,则元组可变 ''' test_tuple=(1,2,3,4,5,['a','b']) res=test_tuple[5] res[0]=6 print(test_tuple) #1.可变元组:元组里面的元素不可变,则元组不可变 test_tuple=(1,2,3,4,5,['a','b']) res=test_tuple[2] res[0]=6 print(test_tuple)
python入门之元组_05
最新推荐文章于 2022-09-26 16:09:19 发布