python 3 基础之简单的组包和拆包

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

 

posted @ 2020-07-05 20:24  吾言!  阅读(773)  评论(0编辑  收藏  举报