拆包和装包

#拆包
t1=(4,7,3)
#a,b=t1  #valueError: too many values to unpack(拆包) (expected(希望,盼望)  2)

a,b,c=t1
print(a,b,c)
a=t1
print(a)

#变量个数与元组个数不一致
t1=(2,5,8,9,7)
a,*_,c=t1
print(a,c,*_)

a,c,*_=t1
print(a,c,_)
a,b,*c=t1
print(a,b,c)

t1=(9,4,8,6)
a,*b=t1
print(a,b) #*b  表示未知个数0-n,  0--[]   多个元素的话  [1,2,3,4,....]
print(*b)
print(b)

'''
字符串  x,y,z='hello'  ----->x='h'   y='e'  z=['l','l','o']
列表   x,y,z=['aa','6','hello','good','happy','lucky'] ---> x='aa'  y=6  z=['hello','good','happy','lucky']

执行结果:

4 7 3
(4, 7, 3)
2 7 5 8 9
2 5 [8, 9, 7]
2 5 [8, 9, 7]
9 [4, 8, 6]
4 8 6
[4, 8, 6]

底层分析:
在这里插入图片描述

发布了41 篇原创文章 · 获赞 1 · 访问量 681

猜你喜欢

转载自blog.csdn.net/qq_41543169/article/details/105085762
今日推荐