14-元组

作者: 努力爬行中的蜗牛 | 来源:发表于2018-10-26 15:11 被阅读6次
    元组定义

    元组(Tuple)和列表类似,不同之处在于元组不能修改。

    • 用于存储一窜信息,数据之间用,分开
    • 元组用()定义
    • 元组的索引从0开始
    tuple_list = ("zhangsan","lisi","wangwu")
    info_tuple = ("zyx",18,1.75)
    print(info_tuple[0])
    print(info_tuple[1])
    #元组只包含一个元素时,需要在后面跟上,
    one_tupel = ("onlyone",)
    print(type(one_tupel))
    
    元组的常用操作
    info_tuple. 
                         info_tuple.count   
                         info_tuple.index   
    
    • 取索引
    info_tuple = ("zyx",18,1.75)
    print(info_tuple.index("zyx"))
    
    • 统计元素在元组中出现的次数
    print(info_tuple.count(18))
    
    元组的循环遍历
    for temp in info_tuple:
        print(temp)
    
    元组的应用场景
    • 作为函数的参数和返回值
    • 格式化字符串,格式化字符串后面的()本质就是一个元组
    • 让列表不可以被修改,保护数据安全
    print("姓名:%s,年龄:%d,身高:%.2f",info_tuple)
    
    元组与列表之间的转换
    info_list = list(info_tuple)
    info_yuanzu = tuple(info_list)
    

    相关文章

      网友评论

          本文标题:14-元组

          本文链接:https://www.haomeiwen.com/subject/dfoktqtx.html