Return value of the function parameters and
The default parameters
1
2
3
4
5
6
7
8
9
10
|
def
stu_register(name,age,country,course):
print
(
"----注册学生信息------"
)
print
(
"姓名:"
,name)
print
(
"age:"
,age)
print
(
"国籍:"
,country)
print
(
"课程:"
,course)
stu_register(
"王山炮"
,
22
,
"CN"
,
"python_devops"
)
stu_register(
"张叫春"
,
21
,
"CN"
,
"linux"
)
stu_register(
"刘老根"
,
25
,
"CN"
,
"linux"
)
|
The basic parameters are found in country "CN", as we registered users on the site, such as citizenship information, you do not fill, the default will be China, which is implemented by default parameters, the country become the default parameters very simple
1
|
def
stu_register(name,age,course,country
=
"CN"
):
|
Thus, this parameter is not specified in the call, the default is that CN, if specified, use the value you specify.
In addition, you may have noticed, after the country become the default parameters, I also put it moved to the rearmost position, and why?
key parameter
Under normal circumstances, the parameters passed to the function according to the order, did not want to order you can use the key parameters, simply specify the name of the parameter can be, but remember that a requirement is that the key parameters must be placed after the position parameters.
1
|
stu_register(age
=
22
,name
=
'alex'
,course
=
"python"
,)
|
Non-fixed parameters
If you are unsure of the function when the user wants to define the number of parameters passed, you can use non-fixed parameters
1
2
3
4
5
6
7
8
9
10
|
def
stu_register(name,age,
*
args):
# *args 会把多传入的参数变成一个元组形式
print
(name,age,args)
stu_register(
"Alex"
,
22
)
#输出
#Alex 22 () #后面这个()就是args,只是因为没传值,所以为空
stu_register(
"Jack"
,
32
,
"CN"
,
"Python"
)
#输出
# Jack 32 ('CN', 'Python')
|
You can also have a ** kwargs
1
2
3
4
5
6
7
8
9
10
|
def
stu_register(name,age,
*
args,
*
*
kwargs):
# *kwargs 会把多传入的参数变成一个dict形式
print
(name,age,args,kwargs)
stu_register(
"Alex"
,
22
)
#输出
#Alex 22 () {}#后面这个{}就是kwargs,只是因为没传值,所以为空
stu_register(
"Jack"
,
32
,
"CN"
,
"Python"
,sex
=
"Male"
,province
=
"ShanDong"
)
#输出
# Jack 32 ('CN', 'Python') {'province': 'ShanDong', 'sex': 'Male'}
|
Code examples:
1 def print_info(name,age): 2 print('Name: %s'%name) 3 print('Age: %s'%age) 4 5 print_info('alex',35) #必须参数 6 7 def print_info(name,age): 8 print('Name: %s'%name) 9 print('Age: %s'%age) 10 11 print_info(age=35,name='alex') #关键字参数 12 13 def print_info(name,age,sex='male'): 14 print('Name: %s'%name) 15 print('Age: %s'%age) 16 print('Sex: %s'%sex) 17 18 print_info('alex',35) 19 print_info('wuchao',20) 20 print_info('jinxin',18) 21 print_info('xiaoyu',18,'female ') # default parameters (parameters must be placed in the back) 23 is the Add DEF (X, Y, Z): #low adder 22 is 24 Print (X + Y + Z) 25 26 is the Add (l, 2,3) 27 28 DEF the Add (* args): # variable length parameter (tall adder) 29 #Print (args) 30 = 0 SUM 31 is for in args I: 32 = SUM + I 33 is Print (SUM) 34 is 35 the Add (1,2,3,4,5,6) # is no named parameters passed 36 37 [DEF print_info (* args, ** kwargs) : # variable length parameter (key-value pair) 38 is 39 Print (args) # ( 'Alex', 35, 'MALE') received without args parameter is named 40 print (kwargs) # { ' jop': 'IT', ' hobby ':' girl ',' height ': 188} declared parameters are received kwargs 41 is 42 is 43 is print_info (' Alex ', 35,' MALE ', JOP =' the IT ', Hobby =' Girl ',height = 188) # declared parameter passed is 44 is 45 46 is DEF print_info (** kwargs): # variable length parameter (key-value pairs) 47 48 Print (kwargs) # { 'JOP': 'the IT', 'Hobby': 'Girl', 'height': 188} declared parameters kwargs receiver 49 for I in kwargs: 50 Print ( '% S:% S '% (I, kwargs [I])) 51 is print_info (JOP =' the IT ', Hobby =' Girl ', height = 188) declared parameter is passed # 52 position variable length parameter 53 # 54 # * args discharge on the left, ** kwsrgs placed behind 55 def f (* args, ** kwargs): # None named parameter must be placed on the left, the right place declared parameter 56 is Pass 57 is F (1,2, [3,4, 5], = name 'Alex') 58 59 print_info DEF (Sex = 'MALE', * args, ** kwargs): # default parameters of variable length parameter (key-on) must be placed on the left of the variable length parameter 60 Print (args) 61 is Print 62 is (kwargs) 63 is for I in kwargs: 64 Print ( '% S:% S'% (I, kwargs [I])) 65 print_info ( 'female',1,2,3)3) 66 print_info(1,2,3,'female',name='alex') 67 68 # arguments priority 69 def dfun (name, age = 22, * args, ** kwargs): # parameters must - keyword arguments - variable length (no name parameter) - variable length (Statement parameters)
Function's return value
To get the function of the results, you can put the results returned by the return statement
note:
- Function encountered in the implementation process as long as the return statement stops execution and returns the result, so can also be understood as representing the end of the function return statement
- If the function return is not specified, then the function returns None
- return the plurality of objects, the interpreter will be assembled into a plurality of objects as a whole tuple result output
The default parameters
1
2
3
4
5
6
7
8
9
10
|
def
stu_register(name,age,country,course):
print
(
"----注册学生信息------"
)
print
(
"姓名:"
,name)
print
(
"age:"
,age)
print
(
"国籍:"
,country)
print
(
"课程:"
,course)
stu_register(
"王山炮"
,
22
,
"CN"
,
"python_devops"
)
stu_register(
"张叫春"
,
21
,
"CN"
,
"linux"
)
stu_register(
"刘老根"
,
25
,
"CN"
,
"linux"
)
|
The basic parameters are found in country "CN", as we registered users on the site, such as citizenship information, you do not fill, the default will be China, which is implemented by default parameters, the country become the default parameters very simple
1
|
def
stu_register(name,age,course,country
=
"CN"
):
|
Thus, this parameter is not specified in the call, the default is that CN, if specified, use the value you specify.
In addition, you may have noticed, after the country become the default parameters, I also put it moved to the rearmost position, and why?
key parameter
Under normal circumstances, the parameters passed to the function according to the order, did not want to order you can use the key parameters, simply specify the name of the parameter can be, but remember that a requirement is that the key parameters must be placed after the position parameters.
1
|
stu_register(age
=
22
,name
=
'alex'
,course
=
"python"
,)
|
Non-fixed parameters
If you are unsure of the function when the user wants to define the number of parameters passed, you can use non-fixed parameters
1
2
3
4
5
6
7
8
9
10
|
def
stu_register(name,age,
*
args):
# *args 会把多传入的参数变成一个元组形式
print
(name,age,args)
stu_register(
"Alex"
,
22
)
#输出
#Alex 22 () #后面这个()就是args,只是因为没传值,所以为空
stu_register(
"Jack"
,
32
,
"CN"
,
"Python"
)
#输出
# Jack 32 ('CN', 'Python')
|
You can also have a ** kwargs
1
2
3
4
5
6
7
8
9
10
|
def
stu_register(name,age,
*
args,
*
*
kwargs):
# *kwargs 会把多传入的参数变成一个dict形式
print
(name,age,args,kwargs)
stu_register(
"Alex"
,
22
)
#输出
#Alex 22 () {}#后面这个{}就是kwargs,只是因为没传值,所以为空
stu_register(
"Jack"
,
32
,
"CN"
,
"Python"
,sex
=
"Male"
,province
=
"ShanDong"
)
#输出
# Jack 32 ('CN', 'Python') {'province': 'ShanDong', 'sex': 'Male'}
|
Code examples:
1 def print_info(name,age): 2 print('Name: %s'%name) 3 print('Age: %s'%age) 4 5 print_info('alex',35) #必须参数 6 7 def print_info(name,age): 8 print('Name: %s'%name) 9 print('Age: %s'%age) 10 11 print_info(age=35,name='alex') #关键字参数 12 13 def print_info(name,age,sex='male'): 14 print('Name: %s'%name) 15 print('Age: %s'%age) 16 print('Sex: %s'%sex) 17 18 print_info('alex',35) 19 print_info('wuchao',20) 20 print_info('jinxin',18) 21 print_info('xiaoyu',18,'female') #默认参数(必须放在必须参数的后面) 22 23 def add(x,y,z): #low加法器 24 print(x+y+z) 25 26 add(1,2,3) 27 28 def add(*args): #不定长参数(高大上加法器) 29 #print(args) 30 sum = 0 31 for i in args: 32 sum += i 33 print(sum) 34 35 add(1,2,3,4,5,6) #传入的是无命名参数 36 37 def print_info(*args, **kwargs): # 不定长参数(键值对) 38 39 print(args) # ('alex', 35, 'male') 无命名参数被args接收 40 print(kwargs) # {'jop': 'IT', 'hobby': 'girl', 'height': 188} 声明参数被kwargs接收 41 42 43 print_info('alex', 35, 'male', jop='IT', hobby='girl', height=188) # 传入的是声明参数 44 45 46 def print_info(**kwargs): # 不定长参数(键值对) 47 48 print(kwargs) # {'jop': 'IT', 'hobby': 'girl', 'height': 188} 声明参数被kwargs接收 49 for i in kwargs: 50 print('%s:%s'%(i,kwargs[i])) 51 print_info(jop='IT', hobby='girl', height=188) # 传入的是声明参数 52 53 #不定长参数的位置 54 #*args放在左边,**kwsrgs放在后边 55 def f(*args,**kwargs): #无命名参数必须放左边,声明参数放右边 56 pass 57 f(1,2,[3,4,5],name='alex') 58 59 def print_info(sex='male',*args,**kwargs): # 不定长参数(键值对) 默认参数必须放在不定长参数的左边 60 print(args) 61 62 print(kwargs) 63 for i in kwargs: 64 print('%s:%s'%(i,kwargs[i])) 65 print_info('female',1,2,3) Print_info 66 (l, 2,3, 'FEMALE', name = 'Alex') 67 priority function parameters 68 # 69 def dfun (name, age = 22, * args, ** kwargs): # must Parameter - Key word parameters - variable length (no name parameter) - variable length (statement parameters)
Function's return value
To get the function of the results, you can put the results returned by the return statement
note:
- Function encountered in the implementation process as long as the return statement stops execution and returns the result, so can also be understood as representing the end of the function return statement
- If the function return is not specified, then the function returns None
- return the plurality of objects, the interpreter will be assembled into a plurality of objects as a whole tuple result output