1、组包:

  将多个值同时赋给一个变量时,解释器会进行自动组包操作

num = 1,2,3,4,5,6
print(num)

  实则将多个值赋给一个变量,自动组成的是元组

2、拆包
  将一个容器值(元组),里面的多个数据同时赋值多个变量,解释器会进行拆包操作

# 这是拆包
# 字符串拆包
str = '12345'
a,b,c,d,e = str
print(a,b,c,d,e)

# 列表拆包
my_lsit = [1,3.14,'hello',True]
num,pi,my_str,my_bool = my_lsit
#
num,pi,my_str,my_bool = [1,3.14,'hello',True]
print(num,pi,my_str,my_bool)

# 元组拆包
my_tuple = (1,3.14,'hello',True)
num,pi,my_str,my_bool = my_tuple

# 字典拆包
my_dict = {"name":"老王", "age": 19}
ret1, ret2 = my_dict
# 字典拆包的区别在于赋值的只是key

 

相关文章:

  • 2021-09-29
  • 2021-12-10
  • 2022-12-23
  • 2021-12-04
  • 2021-06-30
  • 2022-02-12
  • 2022-12-23
猜你喜欢
  • 2021-09-30
  • 2022-12-23
  • 2021-04-29
  • 2021-09-12
  • 2022-01-08
  • 2018-10-12
  • 2022-12-23
相关资源
相似解决方案