p = (4, 5)
x, y = p
print(x)
print(y)
data = ["ACME",50,91.1,(2012,12,21)]
name, shares, price, date = data
print(name)
print(date)
print(name,shares,price,date)
s = "hello"
a, b, c, d, e = s
print(a)
data = ["ACME", 50, 91.1, (2012,12,21)]
_, shares, price, _ = data
print(price)
record = ("Dave","[email protected]","773-555-1212","847-555-1212")
name, email, *phone_numbers = record
print(name) --->Dave
print(phone_numbers) ---> ['773-555-1212', '847-555-1212']
**注意:**phone_numbers 这个变量解压出来的永远是列表类型
*trailing, current = [10,8,7,1,9,8,10,3]
print(trailing)
print(current)
records = [
("foo", 1, 2),
("bar", "hello"),
("foo", 3, 4)
]
def do_foo(x, y):
print("foo", x, y)
def do_bar(s):
print("bar", s)
for tag, *args in records:
if tag == "foo":
do_foo(*args)
elif tag == "bar":
do_bar(*args)
line = "nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false"
uname, *fields, homedir, sh = line.split(":")
print(uname)
print(fields)
print(homedir)
print(sh)
record = ("ACME",50,123.45,(12,18,2012))
name, *_, (*_, year) = record
print(name)
print(year)
items = [1,10,7,4,5,9]
def sum(items):
head, *tail = items
return head + sum(tail) if tail else head
print(sum(items))