Shaped Python3 function parameters how to receive arguments?

A: default parameters of the function (the actual call, the number of arguments can be less than the number of formal parameters)

1, grammar:

def function name (parameter name argument default = 1 1 = 2 default parameter name argument 2 ......)

Example:

def info (name, age = 1.address = 'Missing'):

print (name, 'this year', age, 'old, home address is:' address)

info ( 'tarena', 15, 'Chaoyang District')

info ( 'Li', 20)

Description:

The default parameter must exist in order from right to left, if there is a default parameter argument, based on the right of all parameters must have default parameters.

The default parameters can have zero or more, or even all with default parameters.

Exercise:

MySum a write function (), arguments can be passed two or three arguments, if the incoming two arguments, two arguments and, if the incoming three arguments, the solid is returned back to the previous two ginseng and ginseng remainder of the third real results.

II: shape parameter function defined manner

1, the position parameter

grammar:

def function name (parameter 1, parameter 2, ......)

Statement block

2, the asterisk parameter tuple:

grammar:

def function name (parameter name tuple *):

Statement block

effect:

Position to collect the excess mass participation

Description:

Tuple parameter names commonly used: 'args'

Exercise:

In the system are free to enter a number, and this number is obtained, and print output.

3, named key parameter

grammar:

def function name (* named key parameter):

Statement

or

def function name (* args, name keyword parameter):

Statement

effect:

All parameters must be passed by reference parameter passing keyword or keyword dictionary transfer

Example:

4, double asterisk dictionary parameter:

grammar:

def function name (parameter name dictionary **):

Statement

effect:

Keyword collect excess mass participation

Description:

Typically dictionary parameter name as '' kwargs ''

Comprehensive:

Function parameters from left to right are: a position parameter, the asterisk parameter tuple named parameter keyword dictionary and a double asterisk parameter, the function parameters can be used in combination.

Three: the function of variable length parameters:

1, grammar:

def fn(*args, **kwargs)

pass

Description:

You can receive an arbitrary position mass participation and mass participation keywords (like how to pass on how to pass)

Exercise:

Write a myrange function, which returns a list of integers range in line with the rules.

Such as:

L = myrange (3)

Print(L)

L = myrange (3, 6)

Print(L)

说明:首先把终止值设置为None,步长为1,判断是否没有输入终止值,如果没有输入,终止值就等于开始值,初始化开始值为0,定个一个空的列表,然后绑定一个开始值,当开始值小于终止值时,把i添加到列表中,然后输出下一个i的值(i +=i + step),最后把数据传到列表里,代码如下图:

总结:

形参的定义方式一共有4种,可混合搭配使用。大家下去以后一定要多动动笔。推荐我们的Python学习扣qun:784758214 ,看看前辈们是如何学习的!从基础的python脚本到web开发、爬虫、django、数据挖掘等【PDF,实战源码】,零基础到项目实战的资料都有整理。送给每一位python的小伙伴!每天都有大牛定时讲解Python技术,分享一些学习的方法和需要注意的小细节,点击加入我们的 python学习者聚集地

发布了35 篇原创文章 · 获赞 4 · 访问量 3万+

Guess you like

Origin blog.csdn.net/ITHHH777/article/details/104210116