切换视频源:

元组 列表

作者: Hao 编辑: 莫烦 2016-11-03

Tuple

叫做 tuple,用小括号、或者无括号来表述,是一连串有顺序的数字。

a_tuple = (12, 3, 5, 15 , 6)
another_tuple = 12, 3, 5, 15 , 6

List

list是以中括号来命名的:

a_list = [12, 3, 67, 7, 82]

两者对比

他们的元素可以一个一个地被迭代、输出、运用、定位取值:

for content in a_list:
    print(content)
"""
12
3
67
7
82
"""

for content_tuple in a_tuple:
    print(content_tuple)
"""
12
3
5
15
6
"""

下一个例子,依次输出a_tuplea_list中的各个元素:

for index in range(len(a_list)):
    print("index = ", index, ", number in list = ", a_list[index])
"""
index =  0 , number in list =  12
index =  1 , number in list =  3
index =  2 , number in list =  67
index =  3 , number in list =  7
index =  4 , number in list =  82
"""

for index in range(len(a_tuple)):
    print("index = ", index, ", number in tuple = ", a_tuple[index])
"""
index =  0 , number in tuple =  12
index =  1 , number in tuple =  3
index =  2 , number in tuple =  5
index =  3 , number in tuple =  15
index =  4 , number in tuple =  6
"""

降低知识传递的门槛

莫烦很常从互联网上学习知识,开源分享的人是我学习的榜样。 他们的行为也改变了我对教育的态度: 降低知识传递的门槛免费 奉献我的所学正是受这种态度的影响。 通过 【赞助莫烦】 能让我感到认同,我也更有理由坚持下去。