Python-fullstack-s13-day09-python Foundation

Source: Internet
Author: User

First, the function

Function: The function is the encapsulation of a block of code, the program will skip the function when the function is called, we can use the function to encapsulate some fixed function, convenient for us to call later, this measure can make us do not have to repeatedly knock the same code, very development efficiency.

Syntax for the function:

Def func_name ():

Hanshuti (Daimakuai)

Call to function:

function name () # in the present phase, all () can be thought of as a function call

The naming conventions for functions are the same as the naming conventions for variables.

1, letters, numbers, underline

2, can not start with a number, and not all the numbers

3. Cannot be a python keyword

4, not too long

5, to have meaning

6. Cannot use Chinese

7, recommended camel body and underline

1 # the definition of a function and the invocation 2 def Play (): 3     Print ("I want to play basketball") 4 Play ()

return value:

During or after the execution of a function, you can return a result to the caller using return, as long as the program executes to return, the function stops, that is, the statement after the return is not executed, whether in the body of the function or in the body of the loop in the function, Just run to the return and end the function. Return returns a value to the caller of the function, which can be divided into three cases:
1, directly write return or do not write return, do not return any content, the function of the caller received is none.

2, return a variable or value (return value).

3, return returns a number of variables or values, when the interpreter will help us to assemble multiple values into a tuple, and the receiving place can be a variable to accept the tuple, or can be multiple variables to the returned tuple to be deconstructed.

1 #do not write the return of the case2 defPlay ():3     Print("I want to play basketball")4RET =Play ()5 Print(ret)#Print None6 7 #write a return value of the case8 defPlay ():9     Print("I want to play basketball")Ten     return "Ok!,let ' s go" OneRET =Play () A Print(ret)#print Ok!,let ' s go -  - #cases where multiple return values are written to return a tuple the defPlay (): -     Print("I want to play basketball") -     return "ok!","Let ' s go" -RET =Play () + Print(ret)#print (' ok! ', ' let's Go ') -  + #write multiple return values to deconstruct the situation A defPlay (): at     Print("I want to play basketball") -     return "ok!","Let ' s go" -Ret1,ret2 =Play () - Print(RET1)#ok! - Print(Ret2)#Let ' s go


When the function accesses, some information is passed to the function, and the parameters are written in parentheses.

1. Formal parameter: The declaration of the variable given at the position of the function declaration, formal parameters.

2, real parameter: In the function call, the function is passed to the specific value, the actual parameters.

Parameters can have many, separated by commas between each parameter

Actual parameters:

1, positional parameters: The actual parameter is assigned to the formal parameter according to the position.

2, keyword parameters: control parameters, assign values to each parameter.

3, mixed parameters: positional parameters and keyword parameters mixed use, you must write positional parameters before you can make the keyword parameters.

Formal parameters: (there are three kinds of forms, today said two kinds of)

1, Position parameters: So far used are the positional parameters

2. Default value parameter: When there are many duplicate parameters, consider using the default value parameter, the default value parameter must be after the position parameter, when the call is not given the value of the time, will use the default values.



Python-fullstack-s13-day09-python Foundation

Related Article

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: 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.