函数参数调用和非固定参数

def test(x,y,z): #这添加形参

print(x)

print(y)

print(z)

test(1,2,3) # 这里添加实参

1、形参和实参

2、位置参数和关键字

#test(1,2)#位置参数调用:实参与形参一一对应,不能多,不能少
#test(y=1,x=2)#关键字 :与形参顺序无关
#test(y=1,x=2,1)# 关键字参数不能放在位置参数前面
#test(1,z=2,y=5)

3、默认参数:调用函数的时候,默认函数可有可无。

用途:1.默认安装值 2.固定默认值

def test(x,y=2):

print(x)

print(y)

test(1,y=3)

#参数组:参数组要放在最后面如test3(name,x=2,**kwargs)

def test(*gg): #可接受任意数量实参,方成元组的形式

print(gg)

#

test(1,2,3,4,5,5,5,5,5,7,9)

test(*[1,2,3,4,5]) # gg=tuple([1,2,3,4,5])

*args:接受n者位置参数,转换成元组

def test(x,args): # 号代表功能

print(x)

print(args)

#test(1,2,3,4,5,6,7)
#test([1,2,3,4,5,6,7]) #args=*[1,2,3,4,5,6,7]

接受n个关键字参数,转成字典的形式

def test1(**kwargs):!

print(kwargs)

print(kwargs["name"])

print(kwargs["age"])

#

test1(name="alex",age=8) #把n个关键字参数,转换成字典

test1(**{"name":"alex","age":"8"})

位置参数和关键字参数

def test3(name,**kwargs):

print(name)

print(kwargs)

#

test3("alex",age=18,sex="m")

默认参数,位置参数,关键字参数

def test3(name,x=2,**kwargs):

print(name)

print(x)

print(kwargs)

#

test3("alex",age=18,sex="m",x=4)

def test3(name,x=2,*args,**kwargs):

print(name)

print(x)

print(args)

print(kwargs)

#
#

test3("alex",age=18,sex="m",x=4)

猜你喜欢

转载自blog.51cto.com/12992048/2174480