This is the 15th day of my participation in Gwen Challenge

Introduction of parameter

Last time we looked at how parameter passing works, and we looked at several types of parameters.

First, let’s review again, parameters and arguments:

Parameters are defined when a function is defined. They are enclosed in parentheses after the function name and can be null

An argument is passed in a specific parameter value when the function is called

To summarize, whoever calls the function is responsible for passing in the arguments.

Ok, in this period we will learn several parameter types of function in detail, the outline is as follows:

1. Position parameters

Parameter names for Python functions are meaningless. Python allows you to pass in parameter values by name when calling a function.

Positional argument: a parameter passed in according to the position of the parameter

When a function is called, arguments are passed in positional order by default. The number of arguments must also match the number of parameters

Take a little chestnut

Def sum_count(a, b, c): S = a + b + c print(S)Copy the code

An error is reported when the number of arguments does not match the parameters

Sum_count (10, 20Copy the code

2. Keyword parameters

In Python, the involvement of shapes in calling functions is closely related.

Keyword arguments: Pass parameters by parameter names when calling functions, of the form “parameter name = argument”

Keyword parameters, also known as named parameters, are passed without regard to the position and order of the parameters

Take a little chestnut

Def calculate(Length, width): Size = Length * width return size print(" calculate(Length=20, width=60) calculate(Length=20, width=60)) print(" calculate(Length=20, width=60) Calculate (width=5, Length=8) # calculate(width=5, Length=8) #Copy the code

3. Default parameters

Default parameters: When defining a function, we can set specific values for the parameters in advance.

When defining a function, default arguments are placed after other arguments, such as location

The default arguments are optional when a function is called. If a new value is passed, the default value is overridden

Take a little chestnut

Def print_rectangle(weight,height=5): for I in range(height): for j in range(weight): Print ("*", end=" ") print() print(" rectangle ") print(" rectangle ") print(" rectangle ") print(" rectangle ") Print_rectangle (3,9) # override defaultCopy the code

Note that the default value cannot precede the positional argument, or the program will report an error

4. Variable length parameters

Variable length parameters are also known as variable parameters.

An indeterminate parameter refers to a variable number of parameters, in two cases:

  1. * parameter (an asterisk) that collects multiple parameters into a tuple object
  2. The ** parameter (two asterisks) will collect each parameter into a dictionary object

  • A parameter is preceded by a single asterisk, and can be passed tuple and list-type data for the function to process. Take a little chestnut
def print_info(listname, *arg):             
                                            
    print("Listname:", listname)            
                                                                   
    print("arg:",arg)                       
                                                                                 
print_info("student","Tom","Anne","Bob")    
Copy the code

  • A parameter preceded by two asterisks can be passed dictionary type data to the function
def print_dicinfo(Dictname, **arg):                                  
                                                                     
    print("Dictname:", Dictname)                                     
                                                                     
    print("arg:",arg)                                                
                                                                     
                                                                     
print_dicinfo("JueJinginfo",name="JueJing",address="JueJing.cn")     

Copy the code

If the parameter is not long, can I add a parameter?

Let’s look at the example. What happens?

Def test (* a, b, c) : : print (a, b, c) test (2 and 4)Copy the code

Run the above program, and the Python interpreter will report an error

When we call test(2,3,4) and pass in three arguments, the system automatically regards them as values of parameter a, b and c as no values, and considers that the object on which the function was called did not pass enough arguments.

To address this error, Python introduced force-named arguments

Specifies that when you call a function with an indefinite argument followed by a positional argument, you must force the named parameter to be passed as the positional argument.

test(2,b=3,c=4)

Copy the code

5. Reverse parameter collection

Reverse parameter collection passes in arguments to the function for the object

When you call a function whose arguments are tuples, lists, or dictionaries, you can automatically separate the elements by adding an asterisk before the arguments and pass them over to the function for processing

  1. * argument (an asterisk), an argument to a list of separable data types of tuple
  2. ** argument (two asterisks), an argument whose separable data type is dictionary

Take a little chestnut

Def print_info(listName, *arg): print(" listName :", listname) # for var in arg: # print(var) print("arg:",arg) List = ["Tom","Anne","Bob"] print_info("student",*ListCopy the code

Def print_dicinfo(Dictname, **arg): print("Dictname:", Dictname) print("arg:",arg) Dict = {"name":"JueJing","address":"JueJi print_dicinfo("JueJinginfo",**Dict) # Split the argument of the dictionary typeCopy the code

conclusion

In this issue, we have studied several types of parameters in detail, laying a good foundation for our later study of functions.

Practice is the process of testing truth, we practice more hands-on practice, there will be a different wonderful journey ~

Well, that’s the content of this issue, welcome to comment ~