#TupleType.py
#_*_ coding:UTF-8 _*_\
#!/usr/bin/python
'''
Python 元组(Tuple)
1. Python 的元组与列表类似,不同之处在于元组的元素不能修改。
2. 元组使用小括号,列表使用方括号。
3. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
4. 元组中只包含一个元素时,需要在元素后面添加逗号,否则括号会被当作运算符使用:
'''
print("******************** Python 元组(Tuple) **********************************")
tupleA = (125, "SDK", "组件", 1314, "STL", "设计模式")
print("tupleA => ", tupleA)
tupleB = 125, "SDK", "组件", 1314, "STL", "设计模式"
print("tupleB => ", tupleB)
tupleC = () #空元组
print("tupleC => ", tupleC)
print("\n" + "******************** 元组(Tuple) 只有一个元素 ******************************")
tupleA = (13,) #当只有一个元素时,需要在后面添加逗号,否则会被编译器误认为是操作符
print("若=》 tupleA = (13,)")
print("tupleA => ", tupleA)
tupleB = (13) #当只有一个元素时,需要在后面添加逗号,否则会被编译器误认为是操作符
print("若=》 tupleB = (13)")
print("tupleB => ", tupleB)
print("\n" + "******************** 访问 元组(Tuple) ******************************")
tupleA = (125, "SDK", "组件", 1314, "STL", "设计模式")
print("tupleA => ", tupleA)
print("tupleA[2] => ", tupleA[2])
print("tupleA[1:4] => ", tupleA[1:4])
print("tupleA[:5] => ", tupleA[:5])
print("\n" + "******************** 修改 元组(Tuple) ******************************")
tupleA = (125, "SDK", "组件", 1314, "STL", "设计模式")
print("tupleA => ", tupleA)
#tupleA[3] = 125 #组件不支持修改元素值
#print("tupleA => ", tupleA)
print("tupleA[3] = 125 #Error: 组件不支持修改元素值")
tupleB = ("网络编程", 10101)
print("tupleB => ", tupleB)
tupleC = tupleA + tupleB
print("tupleC = tupleA + tupleB")
print("tupleC => ", tupleC)
nA = 7777
tupleD = (nA, "168", "We")
print("tupleD => ", tupleD)
nA = 888
print("tupleD => ", tupleD)
print("\n" + "******************** 删除 元组(Tuple) ******************************")
tupleA = (125, "SDK", "组件", 1314, "STL", "设计模式")
print("tupleA => ", tupleA)
#del tupleA[2] #不支持删除元素
#print("tupleA => ", tupleA)
print("del tupleA[2] #Error: 不支持删除元素")
del tupleA #删除元组
print("del tupleA #删除元组")
#print("tupleA => ", tupleA)
print("\n" + "******************** 元组(Tuple) 运算符 **********************")
tupleA = (125, "SDK", "组件", 1314, "STL", "设计模式")
print("tupleA => ", tupleA)
print("len(tupleA) => ", len(tupleA))
print("(1, 3, \"Touch\") + (\"Test\", 168) => ", (1, 3, "Touch") + ("Test", 168))
print("(12, \"Y\") * 3 => ", (12, "Y") * 3)
print("3 in (1, 7, \"World\", 3) => ", 3 in (1, 7, "World", 3))
print("for x in (125, \"SDK\", \"组件\", 1314, \"STL\", \"设计模式\"): \n\
\t print(x)")
for x in (125, "SDK", "组件", 1314, "STL", "设计模式"):
print(x)
print("\n" + "******************** 元组(Tuple) 元素索引,截取 **********************")
tupleA = (125, "SDK", "组件", 1314, "STL", "设计模式")
print("tupleA => ", tupleA)
print("tupleA[2] =》", tupleA[2])
print("tupleA[-2] =》", tupleA[-2])
print("tupleA[2:] =》", tupleA[2:])
print("\n" + "******************** 元组(Tuple) 内置函数 **********************")
tupleA = ("SDK", "组件", "1314", "STL", "设计模式")
print("tupleA => ", tupleA)
print("len(tupleA) => ", len(tupleA))
print("max(tupleA) => ", max(tupleA))
print("min(tupleA) => ", min(tupleA))
print("\n" + "排序元组元素: sorted()")
print("排序元组元素: 所有元素必须是同一数据类型")
print("sorted(tupleA) => ", sorted(tupleA))
print("\n" + "序列 转 元组: tuple()")
listA = ["SDK", "组件", "1314", "STL", "设计模式"]
print("listA => ", listA)
tupleA = tuple(listA)
print("tupleA => ", tupleA)
Python 元组(tuple)
最新推荐文章于 2024-08-31 21:39:38 发布