About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .
In the actual development process , You will often encounter many identical or very similar operations , At this time , Code that implements similar operations can be encapsulated as functions , Then call the function where you need it . This can not only realize code reuse , It can also make the code more organized , Increase code reliability . Now let's introduce python Function positional parameters for 、 Key parameters , Related contents of variable length parameters .
When you call a function , The order of arguments and formal parameters must be strictly consistent , And the number of arguments and formal parameters must be the same .
example : Run the following program , Analyze the running results .
def printme(a, b, c):
print (a, b, c)
printme(1, 2, 3)
printme(1, 2)
give the result as follows .
Keyword parameter refers to the parameter passing method when calling a function , Is a way to pass values by parameter name . The use of keyword parameters allows the order of parameters in function calls to be inconsistent with the definition ,Python The interpreter can match parameter values with parameter names .
example : Key parameters .
def printinfo(name, age):
print (" name : ", name)
print (" Age : ", age)
return
# call printinfo function
printinfo(age=50, name="runoob")
Usually when defining a function , If you want a function to be able to handle more parameters than it was defined , In this case, the variable length parameter can be used in the function .
def Function name ([ Parameter list ,] *args, **kwargs):
The body of the function
example : Indefinite length parameter .
def f(a, b, *args, **kwargs):
print(a)
print(b)
print(args)
print(kwargs)
f(1, 2, 3, 4, 5, x = 6, z = 7)
give the result as follows .
1、 Liao Xuefeng's official website
2、python Official website
3、Python Programming case tutorial
The above is about python Function positional parameters for 、 Key parameters , Related contents of variable length parameters , You can refer to it , If you think it's good , Welcome to thumb up 、 Collection 、 Looking at , Welcome to wechat search java Basic notes , Relevant knowledge will be continuously updated later , Make progress together .