python\ function

Source: Internet
Author: User

I. Why use a function:
Resolving code reuse issues
Unified Maintenance
The organization structure of the program is clear and readable

Two. Defining functions


First define after use!!
def funcname (Arg1,arg2,...):
"" "Description Information" ""
function body
return value

Defining functions can be divided into three categories:
No parameter function
Foo ()
No return value required
Parameter function
Bar (x, y)
Null function
function Body Write Pass

Three. Calling functions
    Statement form: foo ()
Expression form: res=10*my_sum
Function call as a parameter of another function: print (my_sum)

Four. return value of function
    can return any type
Returns none when no return
Return returns a value
Multiple values can be returned, expressed in tuples
Only one return can be performed
Five. Parameters of the function
Arguments and formal parameters
def foo (x, y)
Print (x)
Print (y)
At the angle of the argument
1. Transfer Values by location
Foo (ON)

2. Pass the value by keyword
Foo (x=1,y=2)

3. mixed with
foo (1,y=2)
The value passed by location must be preceded by the keyword
foo (y=2,1) Error
Span style= "font-family:"microsoft yahei"; Font-size:16px "> can only be assigned one time for a parameter



***** analysis of
positional parameters: parameters that must be passed the value
Default parameters: No need to pass value (can not be transmitted)
Default parameters must be placed after the position parameter

six. *args and **kwargs
*args
* Give him the number after the location value (tuple)
def foo (X,*args):
Print (x)
Print (args)

Foo (1,2,3,4,5)

*args mixing with positional parameters and default parameters

**kwargs
* * Give him the number after the keyword is passed (dictionary)
def foo (X,**kwargs):
Print (x)
Print (Kwargs)
Foo (1,y=1,z=2)


Mix it in.
def foo (X,*args,**kwargs):
Print (x)
Print (args)
Print (Kwargs)

From the angle of the formal parameter
def foo (**kwargs):
Print (Kwargs)
Foo (x=1,y=2,z=3)

Angle of argument
def foo (x,y,z=1):
Print (x)
Print (y)
print (z)

python\ function

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.