python-变量操作-元组

python对变量(不同数据类型)的操作

  • 数字操作
  • 字符串操作
  • 列表操作
  • 元组操作
  • 集合操作
  • 字典操作

  Python3 中有六个标准的数据类型:Number(数字)、String(字符串)、List(列表)、Tuple(元组)、Sets(集合)、Dictionary(字典)

元组操作

  • Python3 元组

    Python 的元组与列表类似,不同之处在于元组的元素不能修改。
    元组使用小括号,列表使用方括号。
    元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。

    如下实例:
    tup1 = ('Google', 'Runoob', 1997, 2000);
    tup2 = (1, 2, 3, 4, 5 );
    tup3 = "a", "b", "c", "d";

    创建空元组
    tup1 = ();

    元组中只包含一个元素时,需要在元素后面添加逗号
    tup1 = (50,);

    元组与字符串类似,下标索引从0开始,可以进行截取,组合等。

  • 访问元组

    元组可以使用下标索引来访问元组中的值,如下实例:

    !/usr/bin/python3

    tup1 = ('Google', 'Runoob', 1997, 2000)
    tup2 = (1, 2, 3, 4, 5, 6, 7 )
    print ("tup1[0]: ", tup1[0])
    print ("tup2[1:5]: ", tup2[1:5])

    以上实例输出结果:

    tup1[0]: Google
    tup2[1:5]: (2, 3, 4, 5)

  • 修改元组

    元组中的元素值是不允许修改的,但我们可以对元组进行连接组合,如下实例:
    #!/usr/bin/python3
    tup1 = (12, 34.56);
    tup2 = ('abc', 'xyz')

    以下修改元组元素操作是非法的。

    tup1[0] = 100

    创建一个新的元组

    tup3 = tup1 + tup2;
    print (tup3)

    以上实例输出结果:
    (12, 34.56, 'abc', 'xyz')

  • 删除元组

    元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组,如下实例:

    !/usr/bin/python3

    tup = ('Google', 'Runoob', 1997, 2000)
    print (tup)
    del tup;
    print ("删除后的元组 tup : ")
    print (tup)

    以上实例元组被删除后,输出变量会有异常信息,输出如下所示:

    删除后的元组 tup :
    Traceback (most recent call last):
    File "test.py", line 8, in
    print (tup)
    NameError: name 'tup' is not defined

  • 元组运算符

    与字符串一样,元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制,运算后会生成一个新的元组。

    计算元素个数

    len((1, 2, 3))
    3

    连接

    (1, 2, 3) + (4, 5, 6)
    (1, 2, 3, 4, 5, 6)

    复制

    ['Hi!'] * 4
    ['Hi!', 'Hi!', 'Hi!', 'Hi!']

    元素是否存在

    3 in (1, 2, 3)
    True

    迭代

    for x in (1, 2, 3): print x,
    1 2 3

  • 元组索引,截取

    因为元组也是一个序列,所以我们可以访问元组中的指定位置的元素,也可以截取索引中的一段元素,如下所示:

    元组: L = ('Google', 'Taobao', 'Runoob')

    读取第三个元素

    L[2]
    'Runoob!'

    反向读取;读取倒数第二个元素

    L[-2]
    'Taobao'

    截取元素,从第二个开始后的所有元素。

    L[1:]
    ('Taobao', 'Runoob!')

    运行实例如下:

    >>> L = ('Google', 'Taobao', 'Runoob')
    >>> L[2]
    'Runoob'
    >>> L[-2]
    'Taobao'
    >>> L[1:]
    ('Taobao', 'Runoob')

  • 元组内置函数

    Python元组包含了以下内置函数

    1 len(tuple) 计算元组元素个数。
    >>> tuple1 = ('Google', 'Runoob', 'Taobao')
    >>> len(tuple1)
    3
    >>>

    2 max(tuple) 返回元组中元素最大值。
    >>> tuple2 = ('5', '4', '8')
    >>> max(tuple2)
    '8'
    >>>

    3 min(tuple) 返回元组中元素最小值。
    >>> tuple2 = ('5', '4', '8')
    >>> min(tuple2)
    '4'
    >>>

    4 tuple(seq) 将列表转换为元组。
    >>> list1= ['Google', 'Taobao', 'Runoob', 'Baidu']
    >>> tuple1=tuple(list1)
    >>> tuple1
    ('Google', 'Taobao', 'Runoob', 'Baidu')

  • Test示例

    '''
    由于元组和数组是类似的,只是其内容不能被修改,所以其操作是有限的
    '''

    tuple1 = (1, 2, 3, 4)
    tuple2 = (2, 3, 4)
    tuple3 = (1,)
    tuple4 = (1) # 这个是不对的,其不会被定义为元组而是单个数组

    print(type(tuple3))
    print(type(tuple4))

    ---取长度 len---

    print('Length of tuple1 is', len(tuple1))

    ---取最大值 max---

    print('Max if tuple1 is', max(tuple1))

    ---取最小值 min---

    print('Min if tuple1 is', min(tuple1))

    ---列表或集合转tuple--- 即序列

    print('List to tuple', tuple([1, 3, 4, 5]))
    print('Set to tuple', tuple(set((1, 3, 4, 5))))

    ---tuple的更新---

    print('tuple1 + tuple2 is', tuple1 + tuple2)

    非法操作

    tuple[0] = 2 不能修改

    ----也不是完全不能修改-----

    tuple_tmp = (1, 2, ['a', 'b'])
    print(id(tuple_tmp[0]))
    print(id(tuple_tmp[1]))

    print('改之前', tuple_tmp)
    tuple_tmp[2][0] = 'b'
    print('改之后', tuple_tmp)
    # tuple_tmp[2] = 'a' 错误
    tuple_tmp[2].append('c')
    print('改之后', tuple_tmp)

    ---这是为什么呢?---

    '''
    元组的一级元素是不能修改的, 像 1 2 ['a', 'b']就是不能修改的,
    对于['a', 'b']来说这个数组你不能将它修改为数字、字符串等,但是你可以对其元素进行修改。
    简单来说就是不能改变元组一级元素的地址
    '''

posted @ 2016-11-22 16:37  银河统计  阅读(437)  评论(0)    收藏  举报