[introduction to Python tutorial] detailed explanation of Python function definition and parameter transfer methods (4 kinds)

Posted by Urbley on Sun, 09 Jan 2022 17:42:56 +0100

This article mainly introduces the detailed explanation of Python function definition and parameter transmission methods (4 kinds). The example code in this article is very detailed, which has certain reference and learning value for everyone's study or work. Friends in need, let's learn together with Xiaobian

1, Elementary knowledge of function

1. Definition: a set of statements is encapsulated by a name (function name). To execute this function, you only need to call its function name.

2. Benefits: code reuse; Consistency; Scalability.

3. Examples are as follows:

|

`# -*-coding:utf-8-*-`

`def` `sayHello():`

`print``(``'Hello world!'``)`

`sayHello()`

|

2, Function parameter transfer mode

For example, the above example is the most basic function and does not pass parameters. Here, we need to know what function parameters are:

1. Function parameters:

Formal parameter variable: the memory unit is allocated only when it is called. At the end of the call, the allocated memory unit is released immediately. Therefore, formal parameters are only valid inside functions.

The formal parameter variable cannot be used after the function call ends and the calling function is returned

Arguments: they can be constants, variables, expressions, functions, etc. no matter what type of quantities the arguments are, they must have definite values in order to pass these values to the formal parameters during function calls. Therefore, the parameters should be determined by assignment, input and other methods in advance.

2. Classification of function parameters

<1> Position parameters

As the name suggests, positional parameters are parameters that correspond one by one to the positions of the arguments passed when calling the function and the formal parameters of the function. For novices, Xiaobai wants to learn Python basics, python crawlers, web development, big data, data analysis, artificial intelligence and other technologies more easily. Here we share the system teaching resources. I Wei (Tongying): 2763177065 [tutorials / tools / methods / troubleshooting]

Examples are as follows:

`#Position parameters`

`print``(``'\n The following are the values of position parameters\n'``)`

`def` `stu_info(name,age,major,country):`

`print``(``'--------Student information-------'``)`

`print``(``'full name:'``,name)`

`print``(``'Age:'``,age)`

`print``(``'Major:'``,major)`

`print``(``'Nationality:'``,country)`

`stu1` `=` `stu_info(``'Jack'``,``21``,``'Chinese'``,``'CN'``)` `# The arguments correspond to name, age, major and country in the function in turn`

`stu2` `=` `stu_info(``'Frank'``,``20``,``'JP'``,``'UN'``)`

`stu3` `=` `stu_info(``'Rose'``,``19``,``'Art'``,``'UK'``)`

|

<2> Default parameters

The default parameter is a little different. It is defined in the function parameter. When the corresponding parameter is not passed in the actual parameter, it comes in handy. It is added to you by default. Isn't it a little sweet?

Note: the default parameter must be placed after the position parameter, otherwise an error will occur

Examples are as follows:

|

|

`#Default parameters`

`print``(``'\n The following are the default parameter values\n'``)`

`def` `stu_info(name,age,major,country` `=` `'CN'``):``# country is set as the default parameter and must be placed after the location parameter, otherwise an error will occur`

`print``(``'--------Student information-------'``)`

`print``(``'full name:'``,name)`

`print``(``'Age:'``,age)`

`print``(``'Major:'``,major)`

`print``(``'Nationality:'``,country)`

`stu1` `=` `stu_info(``'Jack'``,``21``,``'Chinese'``)` `# The corresponding value is not passed here, but it has been defined in the formal parameter, so don't worry about losing your home`

`stu2` `=` `stu_info(``'Frank'``,``20``,``'JP'``)  ``# You too`

`stu3` `=` `stu_info(``'Rose'``,``19``,``'Art'``,``'UK'``)  ``# Now that you've passed it on, it's up to you`

|

<3> Key parameters

Under normal circumstances, the parameters passed to the function should be in order. If you don't want to be in order, you can use the key parameters. You only need to specify the parameter name (the parameter specifying the parameter name is called the key parameter), but remember that the key parameters must be placed after the position parameters (the parameters with corresponding relationship determined in position order). For novices, Xiaobai wants to learn Python basics, python crawlers, web development, big data, data analysis, artificial intelligence and other technologies more easily. Here we share the system teaching resources. I Wei (Tongying): 2763177065 [tutorials / tools / methods / troubleshooting]

Here are some examples:

|

`#The key parameter cannot be assigned repeatedly, and the specified parameter should be after the position parameter`

`print``(``'\n The following are the values of key parameters\n'``)`

`def` `stu_info(name,age,major,country` `=` `'CN'``):` `#`

`print``(``'--------Student information-------'``)`

`print``(``'full name:'``,name)`

`print``(``'Age:'``,age)`

`print``(``'Major:'``,major)`

`print``(``'Nationality:'``,country)`

`stu1` `=` `stu_info(``'Jack'``,``21``,``'Chinese'``)`

`stu2` `=` `stu_info(``'Frank'``,``20``,major``=``'JP'``)` `# major is the key parameter, which is specified separately, but must be placed after the position parameter`

`stu3` `=` `stu_info(``'Rose'``,``19``,``'Art'``,country``=``'UK'``)`

|

<4> Non fixed parameter transfer

This way of transmitting parameters is very particular. There are many patterns and most natural skills. If you don't believe it, look down.

It can be divided into two categories:

Non fixed parameter transmission mode 1:

Multiple users can be specified at the same time, and all parameters transmitted are packaged into Yuanzu. As follows:

#-*-coding:utf-8-*-

#Mode 1

`def` `send_alert(msg,``*``users):` `# '* users' refers to non fixed parameters. Multiple users can be specified at the same time. All parameters passed are packaged`

`for` `u` `in` `users:`

`print``(``'call the police! Please check it in time...'``,u,msg)`

`send_alert(``'Your system is on the verge of collapse!'``,``'cc'``,``'hyt'``,``'yuq'``,``'dy'``)`

#Mode II

`def` `send_alert(msg,``*``users):` `# '* users' refers to non fixed parameters. Multiple users can be specified at the same time. All parameters passed are packaged`

`for` `u` `in` `users:`

`print``(``'call the police! Please check it in time...'``,u,msg)`

`send_alert(``'Your system is on the verge of collapse!'``,``*``[``'cc'``,``'hyt'``,``'yuq'``,``'dy'``])`

`'''`

`If the list is not preceded by * The whole list will be treated as an element in the tuple,`

`plus * After the number, it is equivalent that each element in the list is an element of Yuanzu. It sends a message to every member in the tuple.`

`'''`

`def` `send_alert(msg,``*``users,age):`

`for` `u` `in` `users:`

`print``(``'call the police! Please check it in time...'``,u,msg)`

`send_alert(``'Your system is on the verge of collapse!'``,``'sc'``,``'qh'``,age``=``22``)`

`'''`

`'*users'At this time, all elements after the previous location parameter will be obtained by default. If the key parameter is not specified at the end of parameter transmission, an error will be reported, age Failed to get pass through value`

`'''`

|

**Non fixed parameter transfer mode 2:**

Multiple users can be specified at the same time, and all parameters passed are packaged into a meta group or dictionary. As follows:

`#-*-coding:utf-8-*-`

`def` `func(name,``*``args,``*``*``kwargs):` `# The formal parameters are positional parameters, tuples and dictionaries`

`print``(name,args,kwargs)`

`func(``'Hope'``,``22``,``'CN'``,``'tomorrow'``)` `#Output: Hope (22, 'CN', 'tomorrow') {}`

`func(``'Try'``,``21``,``'will'``,addr``=``'HG'``,num``=``666``)` `#Output: Try (21, 'will') {'addr': 'HG', 'num': 666}`

`dit` `=` `{``'major'``:``'Math'``,``'interest'``:``'reading'``}`

`func(``'want'``,``*``[``'day'``,``'up'``],``*``*``dit)` `#Output: want ('day ',' up ') {' major ':'math', 'interest':'reading '}`

 |

Guys, come here and you'll understand the common parameter passing methods of functions in Python? Is it a long pose again!

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support the introduction of python tutorial.

Topics: Python Back-end crawler Data Analysis