What is the function of python3? In python3, the function and how it is passed as a parameter?

A: function (function)

1. What is the function?

Function blocks can be repeated is performed, it can be reused.

2, the role:

Statement block for enclosing, improve the reusability of code.

Definition of user-level functions.

3, function definition (creation) statement

def syntax statements:

def function name (parameter list)

Statement block

4, instructions:

Name is the name of the function statement block.

The same naming function name and variable name (function name must be an identifier).

Function has its own name space, access to the internal variables can not function outside the function, the internal functions can access external variables function normally allow external data processing functions need to pass parameters to a function with some data.

Function parameter list can be empty.

Statement section can not be empty. If you need to fill the empty pass statement.

5, an example of a function:

Definition of a function, three binding statement block, and outputs the result.

Two: the calling function

1, the name of the function (call transfer actual parameter list) Note: After the call to pass a parameter called the actual arguments.

2, Description:

Function call is an expression

If no return statement, this function after the implementation of a returning None objects, if the function needs to return other objects need to use the return statement.

Example:

def mymax(a,b):

print(‘a=’, a)

print(‘b=’, b)

if a > b:

print (a, 'greater than', b)

else:

print (a, 'less', b)

mymax(20, 30)

Case Study: The above example code, called the parameter in the function name in parentheses (also called variable), the rest are called a block. Parameter does not directly assignment, there are two parameters, when the function call, must give two arguments. Which is to transfer the grammatical structure of a and b values ​​(for a 20, b 30 a, successively in the order of the correspondence), and then determination. FIG code is as follows:

Three: return statement

grammar:

return [Expression] --- [] represents the content of which may be omitted.

effect:

For the function, the end of the execution of the current function, return to where the function is called and returns an object reference relationship.

Description:

return statement followed by the expression can be omitted, the omission is equivalent to return None.

If the function does not return statement, the function returns None After calling the last statement.

函数调用一定会返回一个对象的引用。(默认返回Node)。

示例:

写一个函数mymax,实现返回两个数的最大值。如:

def mymax(a, b)

print(mymax(100, 200))

print(mymax(50, 10))

print(mymax(‘ABC’, ABCD’))

案例分析:首先判断两个数的大小,如果a大于b,就return a,否则就return b。

写一个函数input_number,此函数用来获取用户循环输入的整数,当用户输入负数时,结束输入。将用户输入数以列表的形式返回,再用再建函数max,min,sum求出用户输入的最大值,最小值及和。

案例分析:首先在函数内部定义一个空列表,在输入端输入一个数,并转换为整型, 判断输入的数字如果小于0,返回列表,否则把大于0的数添加到列表中,然后分别求出列表的最大值,最小值与和。代码如下图:

四:python函数的参数传递

1、参数传递方式

位置传递

实际参数(实参)的对应关系与形式参数(形参)的对应关系是按位置来依次对应的。示列:

def myfun(a ,b ,c)

pass

myfun(1,2,3)

说明:

实际参数和形式参数通过位置进行传递的匹配实参个数必须与形参个数相同。

序列传参

序列传参是指在函数调用过程中,用 * 将序列拆解后按位置传参的方式进行参数传递。

示例:

def myfun(a, b, c):

pass

s = [1,2,3]

myfun(*s) 表示把s拆开

s2 = ‘ABC’

myfun(*s2)

关键字传参

关键字传参是指传参时,按着形参的名称给形参赋值,实参和形参按名称进行匹配。

示例:

def myfun(a,b,c):

pass

myfun(b=22,c=33,a=11)

等同于myfun(11,22,33)

Myfun(c=3,b=2,a=1)

字典关键字传参

是指实参为字典,将字典用 ** 拆解后进行关键字传参。

示例:

def myfun(a, b, c):

Pass

d = {‘c’:33, ‘b’:22, ‘a’: 11}

myfun(**d) 拆解字典后再传参

说明:

字典的键名和形参名必须一致。

字典键名必须为字符串。

字典的键名要在形参中存在。

函数的综合传参

函数传参方式,在能确定形参能唯一匹配到相应实参的情况下可以任意组合。

示例:

def myfun(a, b, c):

Pass

myfun(100, *[200, 300]) #正确

myfun(*’AB’,300) #正确

myfun(100, c=300, b=200) #正确

myfun(1, **{‘c’: 3, ‘b’: 2}) #正确

myfun(**{‘c’: 3, ‘b’: 2}, a=1) #正确

myfun(b=2, c=2, 1) #错误,不能确定1给谁?

说明:

传参时先位置传参,后关键字传参。


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

总结:

函数可是python语言中的重中之重,如团队协作开发时,可互相调用编写的函数,这可以得高效率开发的效果。朋友们,有关函数我们就说到这,如果大家喜欢,请互粉、关注、评论!一定会创作更多优质的内容为广大python爱好者们服务。

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

Guess you like

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