Functions of topics python

I. tissue function is good, reusable, code segments used to implement a single, or the associated function. With the function, we can reduce the number of times copy and paste the code to a great extent, we can put the same code can be extracted to make a function where needed to just call. So, thus improving the reuse of code, and the overall code looks more concise.
Below we explain the knowledge about the function:
function is the most basic program structure in Python, to maximize make our code reuse; at the same time, the function can be a complex system is divided into manageable and more parts, simplify programming, code reuse.

1. more concise speaking, what is the function?

函数:就是一个功能,一个解决某一个问题的一个过程

(C language is a process-oriented programming language, is a function of the process herein, a given target into N → → eleven small to achieve the target)

2. Why use the function

有可能重复的代码封装起来,起一个名字,当用到这段代码时调用这个名字,本质是这段代码被调用了,这段代码具有特定功能,用来解决一个或一类问题。

How 3.python defined functions
# rules: def ===> define function defined function
naming names defined functions and variables are consistent #

 def 函数名称([参数列表…])
	函数体
	return 返回值

function block is a standard

4. How do you call a function

函数名称([实参列表])

When the function is defined, for more flexible and convenient functions, some parameters can not be written to determine the value of

The type of function
can be summarized as a function of the type under the following conditions:

1.函数有没有参数,可以将函数分为有参函数和无参函数
2.函数有没有返回值
3.函数的定义者:
	1>.通过有无参数
		①有参函数
		②无参函数
	2>.通过有无返回值
		①有返回值
		②无返回值
	3>.第三方定义的函数(开元公司,机构,组织)

If we need to function operation, and finally need to get the operation result, then we need the functions defined function that returns a value

interpretive code is executed from the top down

3. Global and local variables

函数内部定义的变量叫做局部变量
定义在函数外部的变量叫做全局变量

At this point we have to lead to a knowledge point: the calling function.

Call the function : Call function itself is a push process, such as the function execution is complete, the function will immediately pop (popped) under normal circumstances can not modify global variables, in order to modify the global variables, we have to declare the following statement :

global 全局变量

If forced to modify global variables results are given as the problem push the stack, a short operating life cycle can not be changed for a long life cycle of the global variable (operation function inside will push the stack), as shown below:
Here Insert Picture Description
given as follows :
Here Insert Picture Description

Should be replaced
Here Insert Picture Description
Results:
Here Insert Picture Description
Second passed by reference (passed is the address)

Here Insert Picture Description
a last address of the test is shown in
Here Insert Picture Description
the following reasons:
Here Insert Picture Description
a case is also a function of the
Here Insert Picture Description
Here Insert Picture Description
three function parameters (default parameters, named parameter, the variable parameter, the universal parameter).
When the special function parameter is large, in order to solve the call parameter passing trouble, there are lower:

1.默认值参数:在python中有些参数一般而言,值是固定的,给该值一个默认值,调用这个函数,如果给该参数传递了一个值,则按传递的值算;如果没有传递值,则按默认值算(有默认值的必须放在普通参数的后面)

2.可变参数:在python中,当一个参数特别多时,可以使用可变参数来接收这些参数,* 参数名(不能和其他参数名一样),当不是普通参数时,剩余的参数都会被该可变参数接收(注意,可变参数类型是元组),依然写在普通参数后面
3.命名参数:**参数名,以字典的接收并输出键值对(只匹配键值对)

(Print ( "abc", end = "")) # end = "" was named parameters, default end = "\ n"

4.万能参数:组合参数,当定义函数的时候,有时候需要保留扩展函数功能时,使用可变和命名参数来完成

( , ** parameters and locations are fixed, as is the rest of the parameters for receiving the common parameter)

四种参数的位置是(普通参数-->默认值参数-->可变参数-->命名参数)

Four recursive function
1. recursion: function itself calls itself, in the form of push function call is invoked, but because the stack space is limited, easy to stack overflow
2. In a recursive, if no end condition is a recursive infinite loop
Here Insert Picture Description
five .lambda expression
1. use an anonymous function because python does not support the use of anonymous functions, lambda expressions all introduced
Here Insert Picture Description
standard wording 2. anonymous function:

lambda [参数1,参数2,,,,参数n] :语句
lambda x,y:x+y

Equivalent to ->

def add(x,y)
	return x+y

Note: in python, lambda although simplifies writing code to enhance the maintenance costs of the code, but the code's readability dropped

Published 17 original articles · won praise 2 · Views 369

Guess you like

Origin blog.csdn.net/qq_44487069/article/details/104469246