Introduction to python functions learning summary

1. Representation of functions : use the keyword def to define a function

def test(x): #The x in the tesx brackets is a formal parameter, which can be written or not. Generally speaking, you don't need to write x only when you have written everything in the code block without passing parameters.
		        #This line is empty is the function description, tell the person who takes over what this function is used for
    x = x+1 #code block, many lines
    return x #The return value of the function

2. Formal parameters of functions

Formal parameters are allocated memory units only when they are called, and are released immediately after the call . Therefore, the formal parameter is only valid inside the function. After the function returns to the calling function, the formal parameter cannot be used again. The
actual parameter is the parameter passed to the function when calling, which can be constant, variable, etc.

3. Parameter form

a. Positional and keyword arguments:

       For example, here we have a function def test(x,y,z), when we call this function: test(1,2,3), the interpreter will default to the order x=1,y=2,z=3, At this time, we call this method of positional assignment as positional parameters; in addition, the keyword parameters are: test(x = 1, z = 3, y = 20), we do not need to consider the positional order but pass the parameters through keywords , this is called a keyword argument

b. Default parameters:

        That is, the function will assign a value to a parameter by default. When we call the parameter, we can assign another value to override its original default value. For example: def test2(x, y = None) When we call this function, we can not assign a value to Y, y has a default value of none, but once we assign a value to y, this value will override the default value of y

c. Parameter group: 

        For example def test3( x,*args,**kwargs ):

Here, the *args and **kwargs, these two are special, no error will be reported even if you do not give them values ​​when calling the function; these two formal parameters, the preceding *args can read all the elements of the dictionary,
The . Overall reading (when you are passing actual parameters and do not want *args to be read as a whole, you can add a * in front of your actual parameters, the function of this * is to let *args traverse the actual parameters you pass, and add it one by one), and output it as a tuple. For
example :
def test (x,*args,**kwargs):
    print(x)
    print(args)
    print(kwargs)
test (1, {'acv': 456}, [5,6,8,1,3], (4,8,6,), 2,3,5,8,1,5,4, {33: 'acr'}, y = 2, z = 3)
#test(1,*(2,3,4,8,9,6,2))
===>1
    ({'acv': 456}, [5, 6, 8, 1, 3], (4, 8, 6), 2, 3, 5, 8, 1, 5, 4, {33: 'acr'} ) # outputs a tuple
    {'y': 2, 'z': 3} #y and z are finally turned into dictionaries

3. Function call: just write the function name directly

The value returned by test(2)
is a result of the function. A function without return is called a 'procedure'.
Return value:
When =0, when returning none
=1, when returning object
>1, returning a tuple 4. Functions can reduce repetition The code, that is to reduce the workload 5. Supplement Then the function is best written concisely, not too long




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325777363&siteId=291194637