Function basis of python basic introduction

**python function** 
1. The def name
The def name creates a new function object at runtime and assigns a variable name.
A def statement can appear where any statement can appear (all language names in python are run in real time, without compiling such a
process), or even nested within other statements.
Example:
def func(x,y):
print(x+y)
------------------Separation line-------------- ----------
if a==1:
def func(x,y):
print(x+y)
else:
def func1(x,y):
print(xy)
Second, function parameter
1 , Formal parameter: the variable name in the circle after the function name when defining the function
2. Actual parameter: the value passed to the function when calling the
function , the number of calls must be the same as the declaration.
**Note: The formal parameters and actual parameters should be consistent when passing
, otherwise an error will be reported. 4. Named parameters: Parameters can be passed in random order, that is, the determined incoming parameter values ​​can be skipped. Pass parameter values ​​that are not passed
def func(x,y):
return x+y
print(func(x=1,y=2))
5. Default parameters: default parameters, generally in order from right to left, if there is no default parameter,
def func(x=1,y =2):
return x+y
print(func())
------------------Separation line---------------- --------
def func(x,y=2):
return x+y
print(func(5))

6. Parameters not counting the length: Sometimes, our function may need more values, but we cannot Determined at the formal parameter, then you need to pass the parameter indefinitely

def func(*var):
print(var)

func(1,2,3)
----------------- -dividing line-----------------------
def func1(**var):
print(var)
func1(a=1,b=2)
return A dictionary, the key is a, the value is 1 and so on
, the return value
1. There is a return value: the key word return, and can return multiple values ​​return (a, b, c), when multiple values ​​are returned, it is a tuple

2, there can be no return value

Guess you like

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