The old boy Python-s9 the Nineth day

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.