Python programming basics: functions (2)

Python programming basics: functions (1)

1. lambda() function

The lambda() function is a simple function method that defines the function on the same line. lambda() actually generates a function object (anonymous function), which is mainly used when a function object is required as a parameter or when the function is simple and used once.
The syntax format of the lambda() function is as follows:

lambda 参数1,参数2...:<函数语句>

The return value of this function is the calculation result of the function statement, and can only be one statement. For example:

lambda x,y:x+y

In the above formula, the formal parameters are x and y, and the return result of the function is x+y.
Insert image description here
We have introduced sorting dictionaries by keys before. If you want to sort by values, you can combine the lambda function and the sorted() function.
Insert image description here
In the above example, there are two parameters in the sorted() function. The first one is the dictionary entry that needs to be sorted, and the latter key represents the sorting key specified during sorting. The lambda() function adds each item in the dictionary to The value of the entry is extracted and then combined with the key as the sorting object.

2. Recursive function

Recursion refers to the process of repeating the same method, while a recursive function is a process in which a function continuously calls itself under given termination conditions. The most common one is the process of finding factorials.
Under the premise of non-negative integers, when n is equal to 1, the factorial of n is 1. When n is greater than or equal to 1, n is the result of all positive integers less than or equal to n, that is, n! , and for n, the factorial of n can be regarded as the factorial multiplication of n and n-1, that is, n! =n*(n-1)!, this multiplication process is repeated recursively. Write it in python language, and the content is as follows:
Insert image description here
In the above example, when n is 1, the function fun() returns a result of 1. When n is greater than 1, its factorial will be converted into n-1 Factorial, until the final n value is 1, the process is a recursive process, and the final termination condition is n==1.

In addition to the above examples, it is common to use recursive functions to find the Fibonacci sequence. In the Fibonacci sequence, the first two numbers are both 1. Starting from the third number, each number is equal to the first two. The sum of numbers, according to the above recursive idea, when n takes the value 1 or 2, the recursion ends. When n is greater than 2, n=n-1+n-2, which can be regarded as finding n-1 and n-2 , at this time, the value of parameter n continues to become smaller, and finally converges to n values ​​of 1 and 2. The specific process is as follows:
Insert image description here

3. Scope of variables

During the writing process of a program, we usually need to define relevant variables. These variables will be accessed during the running of the program. However, variables are not accessible from all locations in the program. The access rights depend on whether the variable is Where the assignment is made.
Each variable has its own scope. Scope means where the variable can be accessed and where it cannot be accessed. The two basic variable scopes are "local variables" and "global variables".

Local variables
are directly related to functions. They are usually defined inside the function. Each function call creates a new scope. At this time, local variables can be accessed within the scope. When accessed from outside the function , the variable will fail to access.
Insert image description here
In the above figure, x is a local variable defined inside the function, so when accessed from the outside, a code error will appear. The error content is that the x variable is not defined.

All variables
correspond to local variables. All variables are variables created outside the function, that is, in the main part of the program. They are globally visible.
Insert image description here
In the above figure, x=1 is a global variable, so the function can access x internally and return the calculation result of x+x. At the same time, the external function can also directly use print to read the value of x and output it.

Global variables and local variables
have the same name. There is a situation where all variables have the same name as local variables. In this case, local variables will be accessed first within the function.
Insert image description here
In the above figure, local variable x=2 and global variable x=1 are defined at the same time. Function f() can access these two variables at the same time. At this time, the two variables have the same name, so they will take priority when function f() runs. When accessing the local variable x=2, that is, the first output is 2+2, and when accessing x directly from the outside, the global variable is accessed, that is, the output of x is 1.

If you want to access global variables first within the function in the above situation, you only need to use the keyword global declaration.
Insert image description here
In the function above, first use global to access all variables x=1, and then use x=2 to reassign x. Therefore, the value of global variable x becomes 2 at this time, that is, the first output is 2+2, and the first output is 2+2. The two output global variables x are also 2.

Guess you like

Origin blog.csdn.net/weixin_42051846/article/details/131828546