热线电话:13121318867

登录
2019-06-24 阅读量: 806
元祖是什么

元组通过圆括号(或者什么都不加)而不是方括号来给出具体的描述:

my_list = [1, 2]

my_tuple = (1, 2)

other_tuple = 3, 4

my_list[1] = 3 # my_list现在是[1, 3]

try:

my_tuple[1] = 3

except TypeError:

print "cannot modify a tuple"

元组是通过函数返回多重值的便捷方法:

def sum_and_product(x, y):

return (x + y),(x * y)

sp = sum_and_product(2, 3) # 等于(5,6)

s, p = sum_and_product(5, 10) # s是15,p是50

0.0000
1
关注作者
收藏
评论(0)

发表评论

暂无数据