Home > Backend Development > Python Tutorial > what is python function

what is python function

silencement
Release: 2020-10-13 10:00:13
Original
42338 people have browsed it

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.

what is python function

# 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

what is python function

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: what is python function

The above is the detailed content of what is python function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template