变量解包

  1. username = ["zaygee", "hoan"]
  2. a, b = username
  3. print(a, b) # zaygee hoan
  4. vara, varb = 0, ''
  5. print(vara, varb) # 0,

星号表达式

variables 作为变量名,会贪婪的捕获多个值,并将捕获到的内容作为列表赋值给variables _ 一般用于作为无用占位

  1. # 列表
  2. a, *b, c = [1, 2, 3, 4, 5, 6]
  3. print(a, b, c) # 1 [2, 3, 4, 5] 6
  4. a, *_ = [1, 2, 3, 4, 5, 6]
  5. print(a) # 1
  6. # 字典
  7. a, *b, c = {"a": 1, "b": 2, "c": 3, "d": 4}
  8. print(a, b, c) # a ['b', 'c'] d
  9. # 元祖
  10. a, *b, c = (1, 2, 3, 4, 5)
  11. print(a, b, c) # 1 [2, 3, 4] 5

布尔值作为整数使用

  1. print(sum([True, True])) # 2
  2. print(sum([False, False, False])) # 0