1. Defining functions
def function name (formal parameter):
function body
return XXX--------The following content is no longer executed
---------------------------------------------------------------------------------------------------------------
2. Execution function
Function name (argument)
---------------------------------------------------------------------------------------------------------------
3. Parameters, arguments (default in order)
---------------------------------------------------------------------------------------------------------------
4. Perform parameter incoming arguments, but not in order
---------------------------------------------------------------------------------------------------------------
5. Functions can have default parameters
---------------------------------------------------------------------------------------------------------------
6. Dynamic Parameters
#动态参数一, the type is Ganso, and the passed parameter is Ganso element
def f1 (*a):
Print (A,type (a))
F1 (123,234,[456123789],{1:2})
#动态参数二, the type is a dictionary, and the passed argument is a dictionary key-value pair
def f1 (**a):
Print (A,type (a))
F1 (k1=123,k2=456)
#万能动态参数-------A * compared to the previous
def f1 (*a,**p):
Print (A,type (a), type (p))
F1 (123,234,[456123789],{1:2},k1=123,k2=456)
---------------------
(123, 234, [456123789], {1:2}), <type ' tuple ' >)
({' K2 ': 456, ' K1 ': 123}, <type ' Dict ' >)
(123, 234, [456123789], {1:2}), <type ' tuple ';, <type ' dict ' >)
---------------------------------------------------------------------------------------------------------------
7. Incoming dictionaries for dynamic parameters, list
def f1 (*args):
Print (Args,type (args))
L1=[11,22,33,44]
F1 (L1)
F1 (*L1)
------------------------
([One, one, one, and], <type ' tuple ' >)
((one, one, one, one), <type ' tuple ' >)
------------------------
def f2 (**args):
Print (Args,type (args))
l1={"K1": "123"}
F2 (L1=L1)
F2 (**L1)
-------------------------
({' L1 ': {' K1 ': ' 123 '}, <type ' Dict ' >)
({' K1 ': ' 123 '}, <type ' Dict ' >)
---------------------------------------------------------------------------------------------------------------
8. Global variables, local variables
p= "Chushujin"
Def func1 ():
#局部变量
A=123
Global P #加上此关键词后, the globals will be modified, otherwise they will not be modified
Print (a)
p= "Zhangyu"
Def FUNC2 ():
Print (P)
Func1 ()
Func2 ()
----------------------
123
Zhangyu
Xi. python function learning