• 数据类型之元组


    元组
    1. 元组书写规范
    users = [11,22,33,"sundy"] # 列表(可变)
    users = (11,22,33,"sundy") # 元组(不可变)
    2.独有功能(无)
    3. 公共功能
    1. 索引(排除:int/bool)
    示例:
    users = (11,22,33,"sundy")
    print(users[0])
    print(users[-1])    
     
    结果:
    D:python3.6python3.6.exe D:/python_code/day01/day04.py
    11
    sundy
     
    2. 切片(排除:int/bool)
    示例:
    users = (11,22,33,"sundy")
    print(users[0:2])

    结果:
    D:python3.6python3.6.exe D:/python_code/day01/day04.py
    (11, 22)
     
    3. 步长(排除:int/bool)
    示例:
    users = (11,22,33,"sundy")
    print(users[0:2:2])
    结果:
    D:python3.6python3.6.exe D:/python_code/day01/day04.py
    (11,)
    4. 删除(排除:tuple/str/int/bool)
    5. 修改(排除:tuple/str/int/bool)
    6. for循环(排除:int/bool)
    示例:
    users = (11,22,33,"sundy")
    for item in users:
        print(item)
    结果:
    D:python3.6python3.6.exe D:/python_code/day01/day04.py
    11
    22
    33
    sundy 
    7. len(排除:int/bool)
    示例:
    users = (11,22,33,"sundy")
    print(len(users))
    结果:
    D:python3.6python3.6.exe D:/python_code/day01/day04.py
    4
     
    4. 特殊:元组中的元素(儿子)不可被修改/删除。
    # 示例一:
    v1 = (11,22,33)
    v1[1] = 999 # 错误
    v1 = 999  # 正确
     
    # 示例二:可以嵌套
     v1 = (11,22,33,(44,55,66),(11,2,(99,88,),3))
     
    # 示例三:嵌套
    v2 = [11,22,33,(11,22,33)]
    v2[-1][1] = 99 # 错误
    v2[-1] = 123 # 正确
     
    # 示例四:嵌套
    v3 = (11,[1,2,3],22,33)
    v3[1] = 666 # 错误
    v3[1][2] = 123 #正确
     
     
  • 相关阅读:
    蓝桥学院2019算法题1.8
    蓝桥学院2019算法题1.7
    蓝桥学院2019算法题1.6
    蓝桥学院2019算法题1.5
    蓝桥学院2019算法题1.4
    蓝桥学院2019算法题1.3
    一个简单java爬虫爬取网页中邮箱并保存
    轻松配置java开发环境
    PDF转word文档
    直接插入排序
  • 原文地址:https://www.cnblogs.com/sundy08/p/11804401.html
Copyright © 2020-2023  润新知