Python uses def to start a function definition, followed by the name of the function, the parameters of the function inside the parentheses, and the implementation code of the function inside the parentheses. If you want the function to have a return value, use return in the logic code in the expressions.
Basic use
def function_name(parameters): expressions
an actual example
def function(): print('This is a function') a = 1+2 print(a)
Above we have defined a function with the name function, the function does not take no arguments, so the parentheses are empty, followed by the function's function code. If you execute the script, you'll find that it doesn't produce any output, because we've only defined the function, not executed it. Now we'll type function() at the Python command prompt, noting that the parentheses are not omitted from the function call. Then the function code inside the function will be executed and the result will be output:
This is a function 3
If we want the script to be called from within the script, we just need to add the function call statement at the end of the script
1 function()
Then the function will be executed when the script is executed.
DEF function parameters
When we use the call function, we want to specify the value of some variables to be used in the function, then these variables are the parameters of the function, when the function is called, pass them in.
Basic use
def function_name(parameters): expressions
The location of parameters is the function's arguments, which are passed in when the function is called.
# Examples
def func(a, b): c = a+b print('the c is ', c)
A function defined here takes two values as arguments, and the function's function is to add up the two arguments. After you run the script and call the function func from within the Python prompt, if you don't specify func(), you'll get an error; the output func(1, 2), passing a=1, b=2 into the function, outputs the c is 3. So when calling a function, the number and position of the arguments must follow the function definition. If we forget the position of the parameters of the function, only know the name of each parameter, you can in the process of the function call to indicate a specific parameter func(a=1, b=2), in this case, the position of the parameters will not be affected, so func(b=2,a=1) is the same effect.
Default Arguments for DEF Functions
We are defining the function sometimes some of the parameters in most cases is the same, but in order to improve the applicability of the function, provides some alternative parameters, in order to facilitate the function call, we can set these parameters as the default parameter, then the parameter in the function call process can not need to be explicitly given.
#Basic use
def function_name(para_1,...,para_n=defau_n,..., para_m=defau_m): expressions
Function declarations are simply given with the = sign wherever a default parameter is required, but note that no default parameter can precede a non-default parameter.
# Examples
def sale_car(price, color='red', brand='carmy', is_second_hand=True): print('price', price, 'color', color, 'brand', brand, 'is_second_hand', is_second_hand,)
Here we define a sale_car function with the car's attributes as arguments, but besides price, things like color, brand and is_second_hand all have default values. If we call the function sale_car(1000), then it has the same effect as sale_car(1000, 'red', 'carmy', True). True). Of course, it is possible to change the default parameters by passing in specific parameters during the function call. The default parameters reduce the complexity of our function calls.
Basic Knowledge Points Supplement:
def
Necessary functions for custom functions: def
Usage:
def function name(parameters1,parameters2,parameters…): function body (math.)(statement block (computing)) return [return value]
caveat
- Naming rules for function names: The basic rules for identifiers are the same, basically consisting of lowercase letters and underscores.
- def is a keyword and cannot be modified (short for define)
- Functions must be followed by parentheses (in English) Whether or not to add parameters inside the parentheses depends on the situation.
- The colon after the parentheses cannot be forgotten
- The function body must be indented (4 spaces recommended)
to this article on python def is to do what the article is introduced to this, more related python def is what the content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!