- _: 单下划线变量名,约定的占位符
变量交换
author, reader = 'piglei', 'raymond'
author, reader = reader, author
变量解包
把可迭代对象一次性赋值给多个变量(长度需相等)
usernames = ['piglei', 'raymond']
author, reader = usernames
# 同样支持在循环语句中使用
for username, score in [('piglei', 100), ('raymond', 60)]:
print(username)
多层嵌套
attrs = [1, ['piglei', 100]]
user_id, (username, score) = attrs
动态解包
当使用 * 号作为变量名前缀(*var),可以贪婪的捕获多个变量
data = ['piglei', 'apple', 'orange', 'banana', 100]
username, *fruits, score = data