zoukankan      html  css  js  c++  java
  • 基准对象object中的基础类型----元组 (五)

    object有如下子类:

    CLASSES
        object
            basestring
                str
                unicode
            buffer
            bytearray
            classmethod
            complex
            dict
            enumerate
            file
            float
            frozenset
            int
                bool
            list
            long
            memoryview
            property
            reversed
            set
            slice
            staticmethod
            super
            tuple
            type
            xrange
    子类

    Python的元组与列表类似,不同之处在于元组的元素不能修改。
    元组使用小括号,列表使用方括号。元组与字符串类似,下标索引从0开始,可以进行截取,组合等。

    1、定义

    #元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
    #如下实例:
    tup1 = ('physics', 'chemistry', 1997, 2000)
    tup2 = (1, 2, 3, 4, 5 )
    tup3 = "a", "b", "c", "d"

    2、元组中的元素值是不允许修改的,但我们可以对元组进行连接组合

    >>>tuple1=(1,2,3)
    >>>tuple2=("a","b","c")
    >>>tuple1+tuple2
    Out[82]: (1, 2, 3, 'a', 'b', 'c')

    3、元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组

    >>>tuple1=(1,2,3)
    >>>tuple1
    Out[88]: (1, 2, 3)
    >>>del tuple1

    4、元组可以使用下标索引来访问元组中的值

    >>>a=(1,2,3,4)
    >>>a[0]
    Out[99]: 1
    >>>a[-1]
    Out[100]: 4
    >>>a[-3:-1]
    Out[102]: (2, 3)
    >>>a[0:2]
    Out[103]: (1, 2)

    T.index(value, [start, [stop]]) -> integer -- return first index of value.Raises ValueError if the value is not present.

    T.count(value) -> integer -- return number of occurrences of value.

  • 相关阅读:
    DI的3种实现方式
    spring ioc的实现方式
    异常:This application has no explicit mapping for /error, so you are seeing this as a fallback.
    maven项目 集成SSM框架
    org.xml.sax.SAXParseException错误
    Redis在web中的应用
    上传下载文件实例(vsftp服务器+nginx)
    Redis的安装与启动
    修饰器-2
    修饰器练习
  • 原文地址:https://www.cnblogs.com/windyrainy/p/10654512.html
Copyright © 2011-2022 走看看