len S = ' jingdong appliance Section ' DEF my_len (): # custom function I = 0 for K in S: I + =. 1 Print (I) my_len () # function call
Function
defines the future, you can call at any place that needs it
does not return the length, simply print
Returns the importance A, B len (A) # built-in function len (B) DEF my_len (): # custom function I = 0 for K in S: I + =. 1 Print (I) return I # Return Value length = my_len () Print (length)
len ()
. 1, can not be changed, only calculate the length of the string
2, but the result is output
three cases # return value
returns no value - returns None
not write return
DEF FUNC (): L = [ ' gold boss ' , ' brother ' ] for I in L: Print (I)
Write only return: the end of a function to continue
DEF FUNC (): L = [ ' gold boss ' , ' brother ' ] for I in L: Print (I) return Print ( " brother brother brother ' )
red = func()
print(ret)
return None - not commonly used
DEF FUNC (): L = [ ' gold boss ' , ' brother ' ] for I in L: Print (I) IF I == ' gold boss ' : return Print ( ' = ' * 10)
It returns a value that
can return any data type
def func(): return [1,2,3] print(func) red = func() print(ret)
As long as the returns can be received
if more than one person return, only the implementation of a program in the first
return multiple values
receiving a number of variables: how much of the return value variable with the number of accepted
def func2(): return 1,2, r1,r2, = frunc2() print(r1,r2)
Receiving a variable: a neuron progenitor is obtained
def func2(): return 1,2,3 r = func2() print(r)
What is the parameter
syntax parameters of said
parameter concepts and arguments
DEF my_len (S): # custom function parameters only 0. Accept parameters, in the form of parameters, parameters, I = 0 for K in S: I + =. 1 return I S = ' jingdong appliance Section ' RET = my_len (S) # pass parameters: transmission parameters, the actual parameters, arguments ret = my_len ([1,2,3,4 ,]) Print (RET)
parameter
No parameters
when defining the function and calling the function in brackets do not write the contents of
a parameter
of what passed is what
multiple parameters
positional parameters
def my_sum(a,b): res = a+b return res ret = my_sum(1,2) print(ret)
Standing on the angle argument: a reference position in accordance with the transmission in accordance with the key parameter passing mix can be: However, according to the need to pass a reference position, then the parameter passing by keyword can not pass a plurality of values to the same variable DEF my_sum (A, B) : Print (A, B) RES = A + B return RES RET = my_sum (. 1, B = 2 ) Print (RET)
Standing on the parameter of the angular position parameters: must pass, and there are several parameters to pass several values default parameters: can not pass, if the default parameter is not passed, if used to pass pass DEF Classmate (name, Sex = ' M ' ): Print ( ' % S:% S ' % (name, Sex)) Classmate ( ' brother ' ) Classmate ( ' small dream ' ) Classmate ( " dream " ) Classmate ( ' Franco ' , ' female ' )
Only when the function is called
According to the position Biography: write directly to the value of the parameter
by keyword biography: keyword = value
Defined function of time
Location parameters: Direct-defined parameters default parameters: Keyword: Parameter name = 'default values' dynamic parameters: You can accept any number of parameters plus before the parameter name * , accustomed to the parameter name args, before the parameter name plus ** , habit parameters name kwargs order: location parameter, * args, default parameters, ** kwargs
def sum(*args): n = 0 for i in args: n+=i return n print(sum(1,2)) print(sum(1,2,3)) print(sum(1,2,3,4)) def func(**kwargs): print(kwargs) func(a = 1,b = 2,c = 3,d = 4) func(a = 1,b = 2,c = 3,)
func(a = 1,b = 2,)
There are two dynamic parameters:
Can take any arguments * args: accepted in accordance with the value of the position parameter passing, organized into a neuron progenitor ** kwargs: accepted by keyword parameter passing values, organized into a dictionary args must precede kwargs DEF FUNC (* args, ** kwargs): Print (args, kwargs) FUNC ( 1,2,3,4, A = ' AAAAA ' , B = ' SSSSSS ' )
Another parameter passing parameters dynamically
DEF FUNC (* args): # standing on the angle parameter, coupled to the variable *, is a combination of all the values coming. Print (args) FUNC ( 1,2,3,4,5 ) L = [1,2,3,4,5 ] FUNC ( * L) # standing on the angle of the argument: * to add a sequence, this sequence is broken up in order DEF FUNC (** kwargs): Print (kwargs) FUNC (A =. 1, B = 2 ) D = { ' A ' :. 1, ' B ' : 2 } FUNC ( ** D )
Notes functions
DEF FUNC (): '' ' This function implements what function Parameter 1 Parameter 2 : return: the length of a string or list # ' '' Pass
function
1. The function definition DEF 2 . Calling function 3. The function returns the value of the return argument 4 function parameter: positional parameters: must pass * args: can take any position of a plurality of parameters default parameters; can not pass ** kwargs: keyword parameters can accept multiple arguments: mass participation by location, by keyword parameter passing