zoukankan      html  css  js  c++  java
  • python学习笔记之--元祖

    元祖tuple 元祖不能改 只能读取     元祖里面存列表/字典(可变类型对象)是可以改变

    定义:a=()


    >>> type(a)#查看类型
    <class 'tuple'>
    >>> isinstance(a,tuple)#判断是否为元祖
    True
    >>> a=1,2#不加括号也可自动转化为元祖
    >>> type(a)
    <class 'tuple'>
    >>> a=(1)#只有一个元素是int类型
    >>> type(a)
    <class 'int'>
    >>> a=(1,)#只放一个元素的元祖必须加逗号

    1. 查

    例:元祖取值

    >>> a=(1,2,[4,5,6])
    >>> a
    (1, 2, [4, 5, 6])
    >>> a[2][0]#嵌套取值
    4
    >>>

    遍历元祖

    按值遍历

    >>> for i in a:
    ...     print(i)
    ...
    1
    2
    [4, 5, 6]
    >>>

    按坐标遍历

    >>> a=(1, 2, [3, 4, 5, 6])

    >>> for i in range(len(a)):
    ... print(a[i],end=' ')
    ...
    1 2 [3, 4, 5, 6]

    >>>

    2.增 只能改变元祖内可变对象的值

    >>> a=(1,2,[3,4,5])
    >>> a[2].append(6)
    >>> a
    (1, 2, [3, 4, 5, 6])

    >>> a.append(0)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: 'tuple' object has no attribute 'append'

  • 相关阅读:
    造数据
    自定义注解
    利用线程池,同步线程实现并发
    ThreadPoolExecutor 线程池
    velocity 模板
    [python] 解析xml文件
    url 中需要转义的字符
    Appium 坑
    TestNG 101
    【python】print · sys.stdout · sys.stderr
  • 原文地址:https://www.cnblogs.com/wenm1128/p/11592323.html
Copyright © 2011-2022 走看看