本文主要介绍两个数据类型tuples和lists。
一、tuples
tuples中可以包含各种类型的数据,它也像string类型一样,不可更改;它是用圆括号表示的。
# Python
te = () # empty tuples
t = (2,"mit",3)
t[0] # evaluates to 2
(2,"mit",3) + (5,6) # evaluates to (2,"mit",3,5,6)
t[1:2] # slice tuple,evaluates to ("mit",)
t[1:3] # slice tuple,evaluates to ("mit",3)
len(t) # evaluates to 3
t[1] = 4 # give error,can't modify object
x = 10
y = 5
(x,y) = (y,x) # used to swap variable values x and y
注意到t[1:2]返回一个("mit",),最后还有一个逗号,这是与string类型做区分,否则("mit&#