''' 元组 ''' #定义方法 def get_num(): # 1.定义一个元祖 tuple_one=("fqs",18,"f",18,18,18,18) print(f"tuple_one元祖:{tuple_one}") # 2.单个元祖内元素要注意 ("fqs") 类型是str 字符串;("fqs",)类型是 tuple 元祖 tuple_2 = ("fqs",) print(f"tuple_2元祖类型:{type(tuple_2)}") #3.嵌套 tuple_3=(("doudou",3,True),("fqs",18,False)) print(f"{type(tuple_3)}") #4.元素的方法 找元祖内元素的下标是多少 仅支持一维数组的下标 index=tuple_3.index(("doudou",3,True)) print(f"index:{index}") #5.查找元素的个数 count_18=tuple_one.count(18) print(f"个数:{count_18}") #6.查元祖的长度 length=len(tuple_3) print(f"元祖的长度{length}") #7.while循环 index=0 while index<len(tuple_one): print(f"第{index+1}个元素是{tuple_one[index]}") index+=1 #8.for循环 for x in tuple_one: print(f"x:{x}") #调用方法 get_num()
标签:基本,index,tuple,18,语法,print,fqs,元祖 From: https://www.cnblogs.com/haha1988/p/17297638.html