1. Definition of function:
def function name (parameter):
function body
return value
Example:
def fuction1 (name):
If name = ' Alex ':
Print (' Success ')
Else
Print (' Failure ')
2. Return value:
Tell the result of the function execution, success or not;
Def F1 (age):
If Age >18:
Return ("adult")
Else
Return ("Flying adult")
3. Parameters
3.1: no parameter function:
def alarm ():
Login mailbox;
Send alarm information;
3.2 Parameter function:
3.2.1: Common parameter function:
#定义函数f1, name is the formal parameter of the F1 function
def f1 (name):
Print (name)
#执行函数, HJW is the actual parameter of the F1 function, called the real argument
F1 (' HJW ')
3.2.2: Default parameter function:
#定义函数f1, Name,age is the formal parameter of the F1 function, and age is also called the default argument, and the default argument must be placed at the end of the argument list
Def f1 (Name,age =20):
Print (Name,age)
#执行函数, HJW is the actual parameter of the F1 function, and age uses the default parameter: 20
F1 (' HJW ')
#执行函数, HJW is the actual parameter of the F1 function, age uses actual parameters: 25
F1 (' HJW ', 25)
3.2.3: Dynamic parameter 1:
#默认参数是按照元祖传入
def func (*args):
Print (args)
# execution Mode One
Func (1,24,6,88)
Execution results: (1, 24, 6, 88)
#执行方式二
Li = [11,22,22,55,55,4,54]
Func (*li)
Execution results: (11, 22, 22, 55, 55, 4, 54)
#执行方式三:
Func (LI)
Execution results: ([11, 22, 22, 55, 55, 4, 54],)
3.2.4: Dynamic Parameter 2:
#默认参数是按照字典, so the arguments are passed in the same way as the key-value pairs;
def f1 (**kwargs):
Print (Kwargs,type (Kwargs))
#执行
F1 (k1=123,k2=456)
#执行结果:
{' K1 ': 123, ' K2 ': 456} <class ' Dict ' >
The basic learning function of Python