1、多重赋值技巧是一种快捷方式,让你在一行代码中,用列表中的值为多个变量赋值。所以不必像这样:
entrepreneur = ['JackMa', 'PonyMa', 'LeiJun']
Alibaba = entrepreneur[0]
Tencent = entrepreneur[1]
Xiaomi = entrepreneur[2]
而可以这样:
entrepreneur = ['JackMa', 'PonyMa', 'LeiJun']
Alibaba, Tencent, Xiaomi = entrepreneur
print(Alibaba, Tencent, Xiaomi)
打印结果:
JackMa PonyMa LeiJun
2、变量的数目和列表的长度必须严格相等,否则 Python 将给出 ValueError:
entrepreneur = ['JackMa', 'PonyMa', 'LeiJun']
Alibaba, Tencent, Xiaomi, Baidu= entrepreneur
执行结果报错:
ValueError: not enough values to unpack (expected 4, got 3)