S = ' acquaintance function and the return value ' only (a) print (referred to as (a)) i = 0 for k in s: i = i + 1 print(i) def my_len(): i = 0 for k in s: i = i + 1 print(i) my_len ()
# Function
After you define, you can call at any place that needs it
does not return the length, simply print
#len ()
1. can not be changed, only calculate the length of the string s
2 only output the results
# Return value
return value of three cases
no return value - returns None
not write return
just write return: to continue to the end of a function
return None - not commonly used
to return a value
can return any data type
as long as the returns can be received
if a plurality of return in a program, only the implementation of a
return value of a plurality of
receiving a plurality of variables: the number of receiving the return value variable number
with a received variable: a tuple is obtained
DEF my_len (S): # custom function requires only parameter 0, the reception parameters, in the form of parameters, parameters, I = 0 for K in S: I + =. 1 return I # return value RET = my_len ( ' gold boss small nurses ' ) # pass parameters: transmission parameters, the actual parameters, arguments RET = my_len ([1,2,3,4,5]) # pass parameters: parameter passing Print (RET)
# Parameters
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
# Standing on the angular argument:
in accordance with the position of the transmission parameters
according to the keyword parameter passing
Hunzhe can be used: it must be according to the position parameter passing, and then pass by keyword parameter
can not pass to a plurality of values for the same variable
# standing parameter the angle
position parameters: must pass, and there are several parameters to pass several values
default parameters: you can not pass, if you do not pass is to use the default parameter, if passed on by pass
# Only when the function is called
by location Biography: write directly to the value of the parameter
by keyword: keyword = value
#-defined function of time:
positional parameters: Direct-defined parameters
default parameters, keyword parameters: parameter name = 'default values'
can accept any number of parameters: dynamic parameters
plus * before the parameter name, parameter name used to args,
before adding ** parameter name, parameter name used to kwargs
order: location parameter, * args, default parameters, ** kwargs
There are two dynamic parameters: can take any arguments
* args: in accordance with the received value of the position parameter passing, organized into a tuple
** kwargs: accepted by keyword parameter passing values, organized into a dictionary
args must before kwargs
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, a sequence to add *, this sequence is broken up in order
# Annotation function DEF FUNC (): '' ' This function realizes what function Parameter 1: Parameter 2: : Return: the length of a string or a list of ''' pass
# Default parameters trap
modified # file
# function
. # 1 defined def function
. # 2 function is called
. # 3 return values return function
. # 4-parameter function
# parameter:
Position # Parameters: must pass
# * args: a plurality of locations may receive any parameter
# default parameters: may not pass
# ** kwargs: may receive a plurality of keyword parameter
# argument: mass participation by position, in accordance with the keyword transmitted reference
# function
# built-in functions
# custom function! ! ! ! !