元组
1、定义:元组类似于列表,但是在创建的时候,元组使用的是(),列表使用的是[ ]。当然也可以使用元组的内置方法tuple()进行创建。两者之间的主要区别是:元组是不变 的,而列表是可变的。
temp = ('hello','world',1,2)
temp1 = ('huan',"le",3,4)
temp + temp1
('hello', 'world', 1, 2, 'huan', 'le', 3, 4)
temp
('hello', 'world', 1, 2)
temp1
('huan', 'le', 3, 4)
temp1*3
('huan', 'le', 3, 4, 'huan', 'le', 3, 4, 'huan', 'le', 3, 4)
temp[1:3]
('world', 1)
x1 = (1)
x1
1
type(x)
int
x = (1,)
type(x)
tuple
x
(1,)
temp2 = ('a','b','d','c')
T = list(temp2)
T
['a', 'b', 'd', 'c']
type(temp2)
tuple
type(T)
list
sorted(temp2)
['a', 'b', 'c', 'd']
T= (1,[2,3],4)
print(T)
T[1][1] = 'huanle'
T
(1, [2, 3], 4)
(1, [2, 'huanle'], 4)