测开之路八十:扩展序列,解包赋值

 

# 位数与变量相等的连续赋值
a, b, c = 'qwe'
print(f'a ==> {a}')
print(f'b ==> {b}')
print(f'c ==> {c}')

 

# 位数与变量不相等时连续赋值报错
a, b, c = 'qweqweqwe'
print(f'a ==> {a}')
print(f'b ==> {b}')
print(f'c ==> {c}')

 

# 解包方法一:用下标赋值
s = 'qweqweqwe'
a, b, c = s[0], s[1], s[2:]
print(f'a ==> {a}')
print(f'b ==> {b}')
print(f'c ==> {c}')

 

# 解包方法二:用*声明变量
s = 'qweqweqwe'
a, b, *c = s
print(f'a ==> {a}')
print(f'b ==> {b}')
print(f'c ==> {c}')
a, *b, c = s
print(f'a ==> {a}')
print(f'b ==> {b}')
print(f'c ==> {c}')

 

# 声明的变量数大于值的个数
a, b, c, *d = 'qwe'
print(f'a ==> {a}')
print(f'b ==> {b}')
print(f'c ==> {c}')
print(f'd ==> {d}')

 

posted @ 2019-08-27 15:14  向前走。  阅读(302)  评论(0编辑  收藏  举报