zoukankan      html  css  js  c++  java
  • 变量类型Tuple

    教程:
    一:元组的创建
        
        元组(tuple)与列表类似,不同之处在于元组的元素不能修改
        (1)tuple写在圆括号之间,元素用逗号隔开
        (2)元组元素的类型可以不同
        (3)一个元素,需要在元素后添加逗号tup = (20,)
        (4)元组也可以被索引和切片,方法一样

    二:元组的索引

        变量[头标:尾标]
        从前到后:0---end
        从后到前:-1---->-len(str)

    三:元组的更新---->元组的值不能修改


    四:元组的删除
        
        元组中的元素不允许删除
        del 删除整个元组


    五:元组操作符

        +     用于组合(连接)元组
        *       用于重复元组
        in 、not in 判断元素是否存在


    六:元组内建函数
        len(tuple)        计算元素的个数
        max(tuple)
        min(tuple)
        tuple(seq)

    七:为什么还要用元组???
        (1)速度快
        (2)写保护
        (3)元组可以作为key
      


    CODE:
    # -----------------------------------------------------------------------------------------------------#
    # 元组的创建
    # -----------------------------------------------------------------------------------------------------#

    my_tuple1 = ('1', "2", 'faith', 'English')
    my_tuple2 = ('I', 'Love', 'Python', 'and', 'C++')


    # -----------------------------------------------------------------------------------------------------#
    # 元组的索引
    # -----------------------------------------------------------------------------------------------------#

    print(my_tuple1)
    print(my_tuple1[2])     # 元组的索引

    print(my_tuple1[1:3])   # 元组的切片


    # -----------------------------------------------------------------------------------------------------#
    # 元组的更新(元组不能更新)
    # -----------------------------------------------------------------------------------------------------#
    # my_tuple1[1] = 'like'
    # print(my_tuple1)

    # -----------------------------------------------------------------------------------------------------#
    # 元组的删除
    # -----------------------------------------------------------------------------------------------------#

    print('del前:', my_tuple2)   # 放在后面验证是否删除
    del my_tuple2

    # -----------------------------------------------------------------------------------------------------#
    # 元组的操作符
    # -----------------------------------------------------------------------------------------------------#

    my_tuple3 = (1, 2, 3) + (4, 5, 6)
    print("元组组合:", my_tuple3)

    print("元组重复:", my_tuple3*3)

    print("元素是否在列表中:", 3 in my_tuple3)

    # -----------------------------------------------------------------------------------------------------#
    # 元组内建函数
    # -----------------------------------------------------------------------------------------------------#
    my_tuple4 = my_tuple3*3
    number = len(my_tuple4)  # 统计出现的次数
    print('time = ', number)













  • 相关阅读:
    类模板机制
    C和C++中const的区别
    bitset
    静态库or动态库?
    多态原理探究
    程序从编译到运行过程
    对象的内存模型
    重载、重写(覆盖)和隐藏
    对继承和派生的理解
    对C++对象的理解
  • 原文地址:https://www.cnblogs.com/faithyiyo/p/9710399.html
Copyright © 2011-2022 走看看