元组(tuple)
元组内的元素不可修改
tuple_info =("dasd",12,4)
#取第一个
tuple_info[0]
#如果我们定义一个元组中只有一个数据那么需要在后面加一个,
tuple_info =(23,)
元组中常用的函数为(元组.count 和 元组.index)只有两种方法
info_tuple = ("zhangsan",2,12,"zhangsan")
#取值和索引
print(info_tuple[0])
#已经知道数据内容,希望知道该数据在元组的索引位置
print(info_tuple.index("zhangsan"))
#统计出现的次数
print(info_tuple.count("zhangsan"))
#统计元组中元素个数用len与列表相似
print(len(info_tuple))
遍历循环
#元组中保存的数据类型是不同的,因此没有办法进行拼接
info_tuple = ("zhangsan",2,12,"zhangsan")
for my_tuple in info_tuple:
print(my_tuple)
应用场景:
函数的参数和返回值,一个函数可以接受任意多个参数,或是一次返回多个数据。
列表不可以修改,以保护数据安全。
格式化字符串
输出字符串为%s %d 输出数字 %f 输出小数 (两位小数就是%.2f)
例如 print("%s 年龄是 %d 身高 %.2f" %("小明",18,1.75))
元组和列表相互转换
list(元组) 将元组转为列表
tuple(列表) 将列表转为元组