2021-08-26 Python之拆包装包

#拆包 装包

'''
a,b=t1 ValueError: too many value to unpack (拆包) expected(期望)
x,y,z=(6,) ValueError: not enough values to unpack expected 3,got 1
'''

1 t1=(2,5,9)
2 a,b,c=t1
3 print(a,b,c)
4 a=t1
5 print(a)

#变量个数与元组个数不一致

 1 t1=(2,3,4,5,6,7)
 2 
 3 a,*_,c = t1
 4 print(a,c,_)
 5 
 6 a,c,*_ = t1
 7 print(a,c,_)
 8 
 9 a,b,*c = t1
10 print(a,b,c)
11 
12 t1=(9,)
13 a,*b = t1
14 print(a,b)
15 print(*b)
16 # *b 表示未知个数 ,多个元素:[1,2,3,4……]

'''
赋值:装包 *y=1,2,3
打印:拆包 print(*[1,2,3]) 1,2,3
'''

# 符号:+     *      is     not       not in       in  

1 t2=(2,3)+(6,7)
2 print(t2)
3 t3=(3,4)*3
4 print(t3)

#系统函数:max() min() sum() len() sorted() tuple()
#元组自带的函数: index() count()

1 print(t2 is t3)
2 print(3 not in t3)
3 print(len(t2))
4 print(tuple(sorted(t2)))

 

posted @ 2021-08-26 13:13  admin-xiaoli  阅读(94)  评论(0编辑  收藏  举报