数据类型之元组

元组
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.6\python3.6.exe D:/python_code/day01/day04.py
11
sundy
 
2. 切片(排除:int/bool)
示例:
users = (11,22,33,"sundy")
print(users[0:2])

结果:
D:\python3.6\python3.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.6\python3.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.6\python3.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.6\python3.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 #正确
 
 
posted @ 2019-11-06 12:10  小蚂农  阅读(109)  评论(0编辑  收藏  举报