6.Python-元组

1.创建元组 

my_tuple=('x','y','z')
print("{}".format(my_tuple))
print("mysql_tuple has {} elements".format(len(my_tuple)))
print("{}".format(my_tuple[1]))
longer_tuple=my_tuple+my_tuple
print("{}".format(longer_tuple))

#结果: 
('x', 'y', 'z')
mysql_tuple has 3 elements
y
('x', 'y', 'z', 'x', 'y', 'z')

2.元组解包 

my_tuple=('x','y','z')
one,two,three=my_tuple
print("{0} {1} {2}".format(one,two,three))
var1='red'
var2='robin'
print("{} {}".format(var1,var2))

#结果 
x y z
red robin

3.元组转换成列表-列表转换成元组

my_lists=[1,2,3]
my_tuple=('x','y','z')
print("{}".format(tuple(my_lists)))   #列表转元组 
print("{}".format(list(my_tuple)))    #元组转列表

#结果
(1, 2, 3)
['x', 'y', 'z']