#*- encoding=utf-8 -*
import sys
print(sys.getdefaultencoding())
def test(x,y,z):
print(x)
print(y)
print(z)
test(1,2,3)
#x,y,z称为形式参数,“1,2,3”称为位置参数,赋值时形参不能写在位置参数前面
test(1,z=7,y=3)
####默认值参数
def test6(x,y=2):
print(x,y)
test6(1,2) #通过位置参数传递
test6(y=3,x=6) #通过关键字参数传递
##传递不固定长度的参数
def test2(*args):
print(args)
test2(1,2,3,4,5)
#传递参数数组
def test3(*args):
print(args)
test3(*[1,2,3,4,5])
##传递字典参数 key:value :**kwargs定义:把n个关键字参数转换为字典的方式
def test4(**kwargs):
print(kwargs)
test4(name='abc',age=18,job='sail')
###位置参数+默认值参数+字典参数
def test5(name,age=18,**kwargs):
print(name)
print(age)
print(kwargs)
test5('YF',20,job='sail',address='BJ')
test5('YF',job='sail',address='BJ',age=28)
局部变量与全局变量:
name='Liu Hu Lan' #全局变量 school='HB' def change_name(name): global school school='kinghua' #如果局部变量想变成全局变量,需要在该局部域(函数体)内用global 声明 print('before_name:',name,',School is :',school) name='Hua Mu Lan' print('after name:',name) change_name(name) print(name,school) ''' 以上返回值: before_name: Liu Hu Lan ,School is : kinghua after name: Hua Mu Lan Liu Hu Lan kinghua '''