Python full stack python basic (eight) function (change at any time)

Source: Internet
Author: User

Why use a function:

1, solve the problem of code reuse

2, improve the maintainability of the code, unified maintenance

3, the organization structure of the program is clear, the readability is strong

Defining functions

First define after use!!!

def funcname (Arg1,arg2,.....) #funcname = function Body

"Description Information"

function body

return value

Define no parameter function no return value

def foo ()

Print ("foo")

Defines an argument function that accepts an externally passed-in value, and returns a value with return

def my_sum (x, y)

Return X+y

Defining an empty function pass

Calling functions

Statement form: foo ()

Expression form: res=10*my_sum

Function call as a parameter of another function: print (my_sum)

function return value

Do not write return----->none

Return a value----> this value

Return multiple values-----> tuples consisting of these multiple values

Parameters of the function

Parameter: parameters to define the function (name)

Arguments: parameters (real numbers) used when calling a function can also define variable values and call functions

My_sum (ON)

a=1,b=2

My_sum (A, B)

The argument is valid in the process of a function call, and it is not valid after it!

Angle of argument:

Value My_sum by location

Value by keyword my_sum (y=1,x=2)

Mixed transfer value my_sum (1,y=2)

From the formal parameter angle:

Positional parameters: def foo (x, Y, z) (more function changes)

Default parameters: Def foo (x,y=1) (the function is not changed much, the number of calls is not many)

When do you use it?

Where are they arranged?

The characteristics of the two?

*args def foo (X,*args)

Value by position, extra value is received by *, form a tuple

**kwargs def foo (X,**kwargs)

By the keyword, the extra value is received by the * *, form the dictionary

def foo (X,*args,**kwargs)

Foo (1,2,3,4,5,6,a=1,b=2,c=3)

X=1

Args= (2,3,4,5,6)

Kwargs={"A": 1, "B": 2, "C": 3}

Python full stack python basic (eight) function (change at any time)

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.