Home>Article>Backend Development> What does def mean in python?
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.
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
Example
def function(): print('This is a function') a = 1+2 print(a)
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
For more Python-related technical articles, please visit thePython Tutorialcolumn 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!