First, the definition of function 1):
# function Definition def Mylen (): """ calculate the length of a S1 """ ' Helloworld' = 0 for inch S1: = length+1 print(length)
Definition:def keyword begins with a space followed by the function name and parentheses (), and finally a ":". DEF is fixed, cannot be changed, must be a continuous def three letters, can not be separated ... They are going to love each other together. Space in order to separate the DEF keyword and function name, you must be empty (four tones), of course, you can empty 2, 3, or how much you want to empty, but normal people still empty 1. Function Name: Functions can only contain strings, underscores, and numbers, and cannot begin with a number. Although the function name can be random, but we give the function name or to be as short as possible, and can express function function parentheses: it must be added, do not ask why there is parentheses, in short, with parentheses on the right! 2) call to function:
# function Call Mylen ()
Call:It is
function name ()Remember to add parentheses, okay? 3) Comment of function:
notes:Each function should have a description of the function and parameters, which should be written in the first line below the function. To enhance the readability of your code. 4) function return value: 4.1 Return function return is a keyword, in pycharm, you will see it turned blue. You have to memorize the word without any bad words. This word translates to "return", so the value that we write after return is called "return value" to study the return value, we also know that there are several cases of return value: No return value, return a value, return multiple values
no return value
4.1.1
1. No return value:
1) Do not write return values (do not write returns)
#function DefinitiondefMylen ():"""calculate the length of a S1"""S1="Hello World"length=0 forIinchS1:length= Length+1Print(length)#function CallStr_len =Mylen ()#because there is no return value, the Str_len is none at this timePrint('Str_len:%s'%str_len)
Note: If you do not write return, the default is to return a none: The first function we write does not write return, which is a case where there is no return value.
2) write-only return value (write returns only)
def Ret_demo (): Print (111) return Print (222= Ret_demo ()print(ret)
Note: Once the return is encountered, the entire function is ended.
3) Return None
def Ret_demo (): Print (111) return None Print (222= Ret_demo ()print(ret)
Note: As in the above two cases, we generally do not write this way.
2. returns a value:
#function DefinitiondefMylen ():"""calculate the length of a S1"""S1="Hello World"length=0 forIinchS1:length= Length+1returnlength#function CallStr_len =Mylen ()Print('Str_len:%s'%str_len)
Note: 1. You can return any data type
3. If there is more than one return in a program, execute only one
# receive with multiple variables: How many return values to receive # with the number of variables received # with a variable receive: Get a tuple
The old boy Python-s9 the Nineth day