立即学习:https://edu.csdn.net/course/play/26755/340122?utm_source=blogtoedu
单星和双星运算符的作用:
1.单星:
def fun(first, *second):
print(first)
print(second)
fun(1, 2, 3, 4, 5)
"""结果:
1
(2, 3, 4, 5) """
2.双星:
def fun(first, **second):
print(first)
print(second)
fun(1, a = 2, b = 3, c = 4, d = 5)
"""结果:
1
{"a": 2, "b": 3, "c": 4, "d": 5} """
合并列表和字典的方法:
1.列表:
(1)单星:
a = [1, 3, 5, 7]
b = [2, 4, 6, 8]
print([*a, *b])
""""结果:
[1, 3, 5, 7, 2, 4, 6, 8] """
(2) + :
a = [1, 3, 5, 7]
b = [2, 4, 6, 8]
print(a + b)
"""结果:
[1, 3, 5, 7, 2, 4, 6, 8] """"
(3) extend :
a = [1, 3, 5, 7]
b = [2, 4, 6, 8]
print(a.extend(b))
"""结果:
[1, 3, 5, 7, 2, 4, 6, 8] """"
2.字典:
(1)双星:
a = {"a": 1, "b": 2}
b = {"c": 3, "d": 4}
print(**a, **b)
"""结果:
{"a": 1, "b": 2, "c": 3, "d": 4} """