Python functions refer to organized, reusable code segments used to implement single or related functions. Python functions include some functions that come with the system, third-party functions, and user-defined functions.
# Functions are small methods or small programs that can implement some specific functions. There are many built-in functions in Python. Of course, with the deepening of learning, we can learn tocreate functions that are useful to ourselves. To simply understand the concept of a function, we write some statements. In order to facilitate the use of these statements, we combine these statements together and give it a name. When using it, you only need to call this name to realize the function of the statement group.
What are Python built-in functions?
Some of the functions that come with the python system are called built-in functions, such as: dir(), type(), etc., and we do not need to write them ourselves. There are also third-party functions, which are functions compiled by other programmers and shared for everyone to use.
How to define a function yourself?
Defining a function requires the use of a def statement. The specific syntax format for defining a function is as shown in the figure
1. It starts with def. It means defining the function
2. There should be a space between def and the function name
3, followed by the function name. We made this name ourselves. greet_user here is the function name
4. The function name is followed by a circle. Parentheses () represent the definition of a function, in which parameters can be added
5. Be sure to add a colon after the parentheses (): this is very important
6. The code block is composed of statements and must be indented , that is, print("hello")
To use this function, call it. Function calls let Python execute the code of a function. To call a function, you can specify the function name and the necessary information enclosed in parentheses. Since this function does not require any information, you only need to enter greet_user() when calling it. It will print hello, as follows:
The above is the detailed content of what is python function. For more information, please follow other related articles on the PHP Chinese website!