[Introduction to Python] Function (6)

      This section will introduce the usage of functions in Python in detail, including function definition, call, parameters, return value, scope, etc.

Overview of functions:

      A Python function is a reusable block of code that encapsulates a specific task. By breaking down a program into smaller, more specific tasks, functions provide an efficient way to organize and manage code, are highly flexible and customizable, can accept any number of parameters, and can have default values. By using functions, you can improve code readability, maintainability, and testability, while also providing the ability to reuse code.

1. Environment configuration

My environmentpython is version 3.7.8. The official download path is as follows:

1.python 3.7.8  

You can directly enter the official website to download and install:Download Python | Python.org

2.The compiler chooses spyder, which can be installed through the pip interface:

 
pip install wheel
 
 
pip install PyQt5
 
 
pip install spyder

2. Definition and calling of functions

1. In Python, we can use the keyword "def" to define a function and uniquely identify a function through the function name and parameter list:

grammar:

def 函数名(参数):
    函数体

Example 1:

def hello():
    print('hello python!')
    print('hello friend!')

It is okay to add no parameters in the parentheses in the above example, but some parameters will be included in large projects.

To call a function, just use the function name and the corresponding parameters. Let’s try calling the above function instance:

Run this function directly through hello(), the output is as follows

Example 2:

def sum(a,b):
    print(a+b)

In the above example, there are two parameters a and b in the brackets. We try to pass the parameters to call the function output.

Use sum(3,4), pass in parameters and run as follows

3. Return value of function

The return value of a function is the value returned by the system to the external caller according to the specific definition of the function after the function is executed. In the Python language, when the function reaches the return statement, the execution is completed and the result is returned at the same time. Therefore, more complex logic can be implemented inside the function through conditional judgment and loop settings, and the expected results can be returned. If there is no return statement, None will be returned by default after all statements in the function body are executed.

Example:

def sum(a,b):
    print(a+b)
    return a+b

Output:

As mentioned above, after passing in the parameters, (return a+b) returns the value of a+b.

4. Built-in functions

The functions that come with the Python language are called built-in functions. These built-in functions effectively encapsulate most common operations and can be called directly, which provides great convenience for development. Since built-in functions are functions built into the Python language, they can be called directly without importing any function library. Commonly used built-in functions are as shown in the figure:

Let's take a look at it through an example:

1.abs(): Returns the absolute value of a number

abs(100)

abs(-100)

abs('100')

Output:

As mentioned above, we return the absolute value of this number by passing parameters to abs(), please pay attention to , abs() must pass and can only pass in one parameter, which must be a number.

2.max(): Take the maximum value among the multiple parameters passed in

max(1,2,3,4,5)


max('123456')


max([12,56,98,102])

Output:

Many built-in functions will be used in subsequent projects. The built-in functions are powerful and understanding and mastering them can greatly improve development efficiency.

5. Anonymous functions

The so-called anonymous function is a function defined in a standard form such as不再使用def语句. Python language often uses lambda to create anonymous functions. lambda is just an expression, and the function body is simpler than the function body defined by def. The syntax of the lambda function is as follows:

grammar:

lambda [arg1[,arg2],....argn]]:expression

Example:

print((lambda x, y, z: x + y + z)(1, 2, 3))

Output:

6. Summary

That’s all the knowledge in this section, please give it a try, thank you!

Guess you like

Origin blog.csdn.net/pengneng123/article/details/134597817