CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day07/tuple.py
Views: 729
1
"""
2
元组的定义和使用
3
4
Version: 0.1
5
Author: 骆昊
6
Date: 2018-03-06
7
"""
8
9
10
def main():
11
# 定义元组
12
t = ('骆昊', 38, True, '四川成都')
13
print(t)
14
# 获取元组中的元素
15
print(t[0])
16
print(t[1])
17
print(t[2])
18
print(t[3])
19
# 遍历元组中的值
20
for member in t:
21
print(member)
22
# 重新给元组赋值
23
# t[0] = '王大锤' # TypeError
24
# 变量t重新引用了新的元组 原来的元组被垃圾回收
25
t = ('王大锤', 20, True, '云南昆明')
26
print(t)
27
# 元组和列表的转换
28
person = list(t)
29
print(person)
30
person[0] = '李小龙'
31
person[1] = 25
32
print(person)
33
fruits_list = ['apple', 'banana', 'orange']
34
fruits_tuple = tuple(fruits_list)
35
print(fruits_tuple)
36
print(fruits_tuple[1])
37
38
39
if __name__ == '__main__':
40
main()
41