将一组值打包到一个对象中,即在圆括号放入一组值!
1.7.1元组与列表的区别:
(1)元组与列表的相同之处:索引、切片、连接
(2)元组创建后无法修改它的内容,即无法替换、删除元素或插入元素
stock = ('GooG',100,490.10) #stock = 'GooG',100,490.10
address = ('ww.python.org',80)#address = 'ww.python.org',80
#person = (first_name, last_name, phone) #person = first_name, last_name, phone
#a = () #0元组
#b = (item
, ) #1元组(注意随后的逗号)
#c = item
, #1元组
#将元组解包为一组变量
#name,shares,price = stock
#host,port = address
#firt_name, last_name, phone = person
filename = "tuple.txt"
tuple = []#空列表
for line in (filename):
fields = line.split(",")
name = fields[0] #提取并转换每个字段
shares = int(fields[1])
price = float(fields[2])
stock = (name,share,price)#创建一个元组
tuple.append(stock)#将元组追加到列表中
print("tuple[%d]" %line)