Python User Manual<5>Function

Table of contents

1. Define function

1.1 Formal parameters and actual parameters

1.2 Pass parameters

1.2.1 Utilizing location transfer

1.2.2 Use keyword delivery

1.2.3 Set the default value of actual parameters

1.2.4 Function definition of unknown face with quantitative parameters

1.2 Return value

2. Module

2.1 Import module import

2.2 Import the specified function from

2.3 Function alias as


1. Define function

def function_name(parameter):
    do sth


>>function(1)

1.1 Formal parameters and actual parameters

Formal parameter: parameter

for function definition

Actual parameter: argument

for function calls

1.2 Pass parameters

When calling a function, each actual parameter must be passed to the corresponding formal parameter.

1.2.1 Utilizing location transfer

def fun(per1,per2):
    do sth
argu1=1
argu2=2
fun(argu1,argu2)

1.2.2 Use keyword delivery

No need to consider the order

def fun(per1,per2):
    do sth
argu1=1
argu2=2
fun(per1=argu1,per2=argu2)

1.2.3 Set the default value of actual parameters

When no actual parameters are entered, the default value is called

def fun(per1=0,per2='sb'):
    do sth

1.2.4 Function definition of unknown face with quantitative parameters

def fun1(*per):
    do sth.
def fun2(**per):
    do sth.

 *Formal parameters: Encapsulate the input actual parameters into a tuple

* *Formal parameters: Encapsulate the input keyword actual parameters into a dictionary

Combining the above knowledge points can prevent errors caused by too many actual parameters.

1.2 Return value

return sth

Can return any kind of value

2. Module

Encapsulate the code segment into a module and save it in the module_name.py file

2.1 Import module import

import file_name

2.2 Import the specified function from

from module_name import function_name1,function_name2

 Import all functions

from module_name import *

2.3 Function alias as

from module_name import function_name as name1

Guess you like

Origin blog.csdn.net/weixin_60787500/article/details/127766544