视频源:

元组 列表

作者: Hao 编辑: UnityTutorial 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
"""

分享到: Facebook 微博 微信 Twitter
如果你觉得这篇文章或视频对你的学习很有帮助, 请你也分享它, 让它能再次帮助到更多的需要学习的人. UnityTutorial没有正式的经济来源, 如果你也想支持 UnityTutorial 并看到更好的教学内容, 赞助他一点点, 作为鼓励他继续开源的动力.

支持 让教学变得更优秀