1. Initial function
How do we implement a piece of code before we have a function? We learned the Len () method before we can find the length of a list, and if there is no Len () method, then we have to write a similar function code to achieve the length of a list.
The code is as follows:
list = [OK, ' hello ', ' abc ']
Count = 0
For I in list:
Count +=1
Print (count)
This code implements the function of Len (), but if there is more than N in a program to test the length of the place, we need to paste n times such code, so what is the disadvantage of such operations?
1. Duplicate code multiple
2. Poor readability
2. Create a function
By defining a function, we can solve two of these problems.
S1 = ' Hello world!!!! '
Def my_len ():
Count = 0
For I in S1:
Count + = 1
# Print (count)
My_len ()
2.1 Syntax format for the function :
def function name (parameter 1, parameter 2, Parameter 3,...): The value returned by the "comment" function body Note: The function name should be able to express the function and meaning
2.2 How to run a function
Function name plus parentheses () call a function, which is the execution function
My_len ()
2.3 Principles followed by function operation
Principle: Define first, then call
- function name, which is a variable name
- A variable must be defined and then referenced, so if you do not define a function and simply refer to the function, it is equivalent to referencing a non-existent variable name
- In the body of the function, do not use the print () method, instead of returning the return value, in order to let the function do the least function, do not let the function do extra work
The return value of the 2.4 function return
Role 1: The function of the return value: when it executes, when it encounters a return value return is the End function
Code:
Def func1 ():
Print (11)
Print (22)
Return
Print (333)
Print (444)
Func1 ()
Role 2: The return value is returned to the caller of the function (performer)
- The code part of the function body can not write return, the return value is none by default
- The return value is written as return none, the return value is None
- Return followed by a single value
- Return followed by multiple values, and multiple values are placed in a tuple returned to the caller of the function
Example: return followed by a single value
Def my_len ():
Count = 0
For I in S1:
Count + = 1
Return 666
Print (My_len (), type (My_len ()))
Example: return followed by multiple values
Def my_len ():
Count = 0
For I in S1:
Count + = 1
Return 666,222,count, ' China '
Print (My_len (), type (My_len ()))
Example: return followed by multiple values and assigned to multiple variables
Def my_len ():
Count = 0
For I in S1:
Count + = 1
Return 666,222,count
Ret1,ret2,ret3 = My_len () # (666, 222, 19,)
Print (RET1)
Print (Ret2)
Print (RET3)
3. Parameters of the function
The parentheses that define the function are the places where the function parameters are placed, when the parameter is called a formal parameter, for example, Def My_len (A, b) #括号中是定义参数的地方
If the function is defined by the physical parameter, the call function when the parentheses also write parameters, which is called the actual parameters, such as arguments, for example: My_len (#括号里面的实参), is the actual number
3.1 Types of arguments
- Position parameters
Requirements: Must be in order to pass the parameter, must one by one correspondence
Example:
def func1 (x, y):
Print (x, y)
Func1 (1, 2)
- Keyword parameters
Requirements: No order, must one by one correspondence
Example:
def func1 (x, Y, z):
Print (x, y, z)
Func1 (y=2,x=1,z=5,)
- Mixed parameters (positional parameters + keyword parameters)
Requirements: The keyword parameter must be behind the position parameter, must be one by one corresponding
Example:
def func2 (ARGV1,ARGV2,ARGV3):
Print (ARGV1)
Print (ARGV2)
Print (ARGV3)
Func2 (1,2,argv3=4)
3.2 Types of formal parameters
- Position parameters
Requirements: Must be in order to pass the parameter, must one by one correspondence
Example:
def func1 (x, y):
Print (x, y)
Func1 (ON)
- Default parameters
Requirements: Must be placed behind the position parameter
Attention:If a formal parameter has a default argument, the argument can be passed without passing the argument, and the value of the default parameter is automatically recognized when the function is run.
If the argument passes a new value for the parameter's default argument, the new value overrides the value of the default parameter
Example: Statistic name
def register (name,sex= ' male '):
With open (' register ', encoding= ' utf-8 ', mode= ' a ') as F1:
F1.write (' {} {}\n '. Format (name,sex))
While True:
Username = input (' Please enter your name:/Q or exit ') #传参时可以不传sex
If username.upper () = = ' Q ': Break
If ' a ' in username:
Sex = input (' Please enter Gender: ') #程序运行中给sex参数重新赋值
Register (Username,sex)
Else
Register (username)
- Dynamic parameters (Universal parameters)
Format:
def my_len (*args, **kwargs)
Role:
In some cases, the number of argument values is not fixed, in order to ensure that the program runs, the parameter needs to be able to receive the extra arguments
A *args in a formal parameter that takes all the extra positional parameters of an actual argument angle and places them in a tuple
A **kwargs in a formal parameter that takes all the extra keyword arguments from the argument's angle and places them in a dictionary
Example:
def func2 (*args,**kwargs):
Print (args) # tuple (all positional parameters)
Print (Kwargs) #字典 (all keyword parameters)
Func2 (1,2,3,4,5,6,7,11, ' John ', ' China ', a= ' ww ', b= ' QQ ', c= ' 222 ')
4. Order of parameters
- Positional parameters-*args-Default parameters
Example:
def func3 (a,b,*args,sex= ' man '):
Print (a)
Print (b)
Print (Sex)
Print (args)
FUNC3 (1, 2, ' China ', ' Tom ', ' Jerry ', sex= ' female ')
- Positional parameter-*args-default parameter-**kwargs
Example:
def func3 (a,b,*args,sex= ' male ', **kwargs):
Print (a)
Print (b)
Print (Sex)
Print (args)
Print (Kwargs)
FUNC3 (1, 2, ' China ', ' Tom ', ' Jerry ', Name= ' Jack ', age=46)
5. Scatter and aggregate functions
- Actual parameter location: Plus * or * * is a scatter function
- Parameter position: Plus * or * * is aggregation function
Example:
def func1 (*args,**kwargs): # The definition of the function * aggregation.
Print (args)
Print (Kwargs)
L1 = [1,2,3,4]
L11 = (1,2,3,4)
L2 = [' Tom ', ' Jack ', 4]
Func1 (*L1,*L2,*L11) # function Execution: * Scatter function.
Python Learning Path-functions