zoukankan      html  css  js  c++  java
  • day05 Python 元组

    1、元组介绍

    元组: 俗称不可变的列表.⼜被成为只读列表, 元组也是python的基本数据类型之⼀, 用⼩括号括起来, ⾥面可以放任何数据类型的数据, 查询可以. 循环也可以. 切片也可以. 但就是不能修改。

    2、元组的切片
    tuple[start:end:step]
     
    3、遍历

    # for循环遍历元组 for el in tu: print(el)

    # 尝试修改元组
    # tu[1] = "test" # 报错 'tuple' object does not support item assignment

    tu = (1, "test", [], "hah")
    # tu[2] = ["fdsaf"] # 这么改不⾏

    tu[2].append("fhb") # 可以改了. 没报错 tu[2].append("sure")

    这⾥元组的不可变的意思是⼦元素不可变. ⽽而子元素内部的子元素是可以变, 这取决于⼦元素是否是可变对象.

    4、注意:
    元组中如果只有⼀个元素. 一定要添加⼀个逗号, 否则就不是元组。

    t1 = (1) # 此时()表示的不是元组,而是优先级的运算符。
    print(type(t1)) # <class 'int'>
    
    t2 = (1,)
    print(type(t2)) # <class 'tuple'>

    元组也有count(), index(), len(),max(),min()等方法

  • 相关阅读:
    Lambda
    Thread&线程池
    异常
    Map
    List and Set
    Collection和迭代器Iterator
    Object类,常用API
    (一)自定义 mybatis 之框架介绍
    Nginx三大功能及高并发分流
    http协议改为https
  • 原文地址:https://www.cnblogs.com/fanghongbo/p/9832221.html
Copyright © 2011-2022 走看看