Python入门:列表、字典拆分参数

列表拆分参数

>>> list(range(3,6))
[3, 4, 5]
>>> args = [3,6]
>>> list(range(args))
Traceback (most recent call last):
  File "", line 1, in <module>
TypeError: 'list' object cannot be interpreted as an integer
>>> list(range(*args))
[3, 4, 5]

字典拆分参数

def people(name,age='0',sex='m'):
    print(name,age,sex)

d = {'name':'slg','age':10,'sex':'f'}

people(**d)

运行结果

slg 10 f

补充

def people(name,age='0',sex='m'):
    print(name,age,sex)

d = {'name':'slg','age':10,'sex':'f'}

people(*d)  #此处使用一个‘*’的是字典参数,看看会出现什么结果

运行结果

name age sex

你可能感兴趣的:(python,python)