Python basics-functions and python Basics
A function is a function block that can be called repeatedly.
I. Definition
Def function name (parameter):... function body... return value
Requirements:
- Def: function keyword
- Function Name: the name of the function used for future calls.
- Function body: code segment for running some operations
- Returned value: whether the function is successfully run or not, or other results must be returned to the caller. By default, None is returned.
- Parameters:
Common Parameters
Default parameters
Dynamic Parameters
####### Define a function ####### name is a formal parameter of a function, or def func (name) for short ): print name ####### execute the function ####### "Callback" is the actual parameter of the function func, referred to as the real parameter func ("Callback ")Common Parameters
Def func (name, age = 20): pring "% s: % s" % (name, age) # specify the parameter func ("Qiutujs", 30) # use the default parameter func ("Qiutujs"). Note: The default parameter must be placed at the end of the parameter list.Default parameters
Def func (* args): print (args) # execution method 1 func (11,22, 33,44) # execution method 2 li = [11,22, 33,44] func (* li) # If no * is added, the function func prints li as an element.
Dynamic Parameters
Def func (** kwargs): print (kwargs) # execution method 1 func (name = "success", age = 33) # execution method 2 li = {"name ": "dimensions", "age": 33, "job": "IT"} func (** li) Note: This dynamic parameter converts the input parameter to the dictionary type, therefore, when you enter a parameter, you must enter it as a key-value pair.Dynamic Parameters
def func(*args,**kwargs): print (args) pring (kwargs)
Dynamic Parameters (Universal parameters)