What does def mean in python?

(*-*)浩
Release: 2019-06-20 11:46:57
Original
155037 people have browsed it

If we use code to implement a small function, but want to reuse it in the program code, we cannot paste the code everywhere in the code, because doing so violates the DRY principle in software engineering. Python provides the def function, which can abstract this part of our function into a function to facilitate program calls, or provide it for use by other modules.

What does def mean in python?

Python uses def to start the function definition, followed by the function name. Inside the brackets are the parameters of the function, and inside are the specific function implementation codes of the function. If you want Functions have return values, and return is used in the logic code in expressions. (Recommended learning: Python video tutorial)

def function_name(parameters):
     expressions
Copy after login

Example

def function():
     print('This is a function')
     a = 1+2
     print(a)
Copy after login

Above we defined a function named function, and the function does not accept Parameters, so the inside of the brackets is empty, followed by the function code of the function. If you execute the script, you will find that there is no output because we only defined the function but did not execute it. At this time, we enter the function call function() in the Python command prompt. Note that the parentheses in the function call cannot be omitted.

Then the function code inside the function will be executed, and the output result is:

This is a function
3
Copy after login

For more Python-related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of What does def mean in python?. 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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!