Functions of the "Python Foundation"

Source: Internet
Author: User

First, define the function

def function name (argument list):    function body
def function (): Pass

Second, formal parameters/actual parameters

X, y is the parameter, the memory unit is allocated only when it is called, and the allocated memory unit is freed immediately at the end of the call. Therefore, the formal parameter is only valid inside the function. The parameter variable can no longer be used after the function call finishes returning the keynote function.
It can be a constant, a variable, an expression, a function, and so on, regardless of the type of argument, and when a function call is made, they must have a definite value in order to pass these values to the parameter. Therefore, the parameter should be determined by the method of assignment, input and so on beforehand.

def Add (x, y    ): Print (x+y) Add (+)

Three, parameter type

#required parameters, for arguments, to pass in the function in the correct order. The number of calls must be the same as when declared. defPerson (name,age):Print('name:%s'%name)Print('age:%s'%Age ) Person (Jack,18)#keyword parameters, for arguments, function calls use the keyword argument to determine the value of the passed parameter. The order of arguments is inconsistent with declaration when a function call is alloweddefPerson (name,age):Print('name:%s'%name)Print('age:%s'%Age ) person ( age=18,name='Jack')#The default parameter, for the formal parameter, or the default if no parameters are passed. defPerson (name, age, sex='male'):    Print('name:%s'%name)Print('age:%s'%Age )Print('sex:%s'%sex) person ('Jack', 18)
#non-fixed parameter, for formal parameter; If your function is not defined by how many parameters the user wants to pass in, you can use the non-fixed parameter#*args will change multiple incoming parameters into a tuple form.#**kwargs will turn multiple incoming parameters into a dict form.defPerson (*args,**Kwargs):Print(Args,kwargs) person ("Jack", 32,"CN","Python", sex="Male", province="Shandong")

  

Functions of the "Python Foundation"

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.