day04基本数据类型 (2)

列表嵌套

users = ["dong",0,True,[11,22,33,"小东"],[1,['dong','oldboy'],2,3]] users[0] users[2] users[0][2] users[3] # [11,22,33,"小东"] users[3][-1] # "小东" users[3][-1][1] # '男' users[3] = 666

2. 元组

元组书写规范

users = [11,22,33,"小东"] # 列表(可变) users = (11,22,33,"小东") # 元组(不可变)

公共功能

索引(排除:int/bool)

users = (11,22,33,"小东") print(users[0]) print(users[-1])

切片(排除:int/bool)

users = (11,22,33,"小东") print(users[0:2])

步长(排除:int/bool)

users = (11,22,33,"小东") print(users[0:2:2])

删除:不可删除

修改:不可修改

for循环(排除:int/bool)

users = (11,22,33,"小东") for item in users: print(item)

len(排除:int/bool)

users = (11,22,33,"小东") print(len(users))

独有功能(无)

特殊:元组中的元素但嵌套的数据除外不可被修改/删除。

# 示例一: 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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wssggx.html