Use of Tuple in Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
>>> a = (1,2)	# a is a tuple

>>> a+1 # error

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

TypeError: can only concatenate tuple (not "int") to tuple

>>> a+(1,) # we have to make sure 1 is not an int or string

(1, 2, 1)

# same with following part

>>> a = (1,2,3)

>>> b = (2,3,4)

>>> a+b

(1, 2, 3, 2, 3, 4)

Tuple最特别在于

tuple元素不允许删除修改,但是可以通过连接tuple来实现简介修改/或转置为list