python用,进行多变量赋值

# 多变量赋值 - 数组形式
data = ['hello', 18, [2002, 2, 2]]
name, age, birth = data
print(name)
print(age)
print(birth)

# 多变量赋值 - 元组形式
data = ('hello', 18, [2002, 2, 2])
name, age, birth = data
print(name, age, birth)
# print(name, age, birth, sep="\n")

# 多变量赋值 - 字符串拆分形式(变量个数需和字符数量一致,否则报:ValueError: too many values to unpack (expected {num}))
msg = 'hello'
a, b, c, d, e = msg
print(a, b, c, d, e)

# 下划线可理解为临时变量
msg = 'hello'
a, _, _, _, b = msg
print(a)
print(b)

# 只给第一个和最后一个变量赋值
a, *_, b = msg
print(a, b)
print(*_)

a = msg[:1]
b = msg[-1:]
print(a, b)


上一篇:javaScript高级教程(八)-----正则表达式温故知新


下一篇:python 生日悖论概率计算。