Python function basics

Source: Internet
Author: User

1. Defining functions

Use the DEF statement, followed by the function name, parentheses, colon, sometimes with arguments in parentheses, then line indent, write function body, return value with return statement

For example:

def Test (x):     if 0 :         return x     Else :         return ABS (x) print (Test (4)) print (Test (-5))

2. Parameters

1) Position parameters

For example:

def func (z/y): Print (     x)    print (y)    print (z) func(1,2,3) func (  5,6)

Execution Result:

1 2 3  "f:/python3/function. py" 7 in <module> func (         5,61'z'

You can see that the parameters entered here are less than one will be an error

2) Default parameters

The default parameters are very useful, for example, when we fill in the information, some of the information is the default, such as your country does not fill in the default is China.

For example:

def func (name,sex,country='China'):    print (Name,sex,country) func ('  devilf','boy') func ('GY ','girl', country='USA')

Execution Result:

C:\Python36\python3.exe f:/python3/ function. Pydevilf boy chinagy girl USA

Places to be aware of when setting default parameters:

First, the default parameter must be after the required parameter

Second, when the function has more than one parameter, you can set the parameter to the default parameter that does not often become

3) Variable parameters

The variable parameter is the number of parameters passed in is not fixed, variable, for example:

def func (*args): print (args) func (1) func (1,2,3,4,5) func ([1,2,3,'a','b','C']) func (1,2,3,'a','a',1)

Execution Result:

C:\Python36\python3.exe f:/python3/function. PY (1,) (1,2,3,4,5)([1,2,3,'a','b','C'],)(1,2,3,'a','a',1)

You can see that the variable parameter output is a tuple, no matter how many parameters we enter, can be output

4) keyword Parameters

Keyword parameters and mutable parameters are similar, except that one output is a tuple, and one is a dictionary, for example:

def func (name,sex,**kwargs):    print (Name,sex,kwargs) func ('devilf','  boy', country='China', age=)

Execution Result:

C:\Python36\python3.exe f:/python3/ function. Pydevilf Boy {'country'  China"age"

5) Parameter combination

The parameter combinations are sequential, in order:

Required parameters, default parameters, variable parameters, keyword parameters

For example:

def func (a,b,c='default', *args,**Kwargs): Print ('a=', a) print ('b=', b) print ('c=', c) print ('args=', args) print ('kwargs=', Kwargs) print ('------1---------') func (1,2) Print ('-------2---------') func (1,2, c=3) Print ('--------3----------') func (1,2, c=3, args= ('python','Java'), Name='Devilf', sxe=' Boy') Print ('---------4------------') func (1,2,3,'a','b','C', n1='name', n2='Sex', n3=' Age')

Execution Result:

C:\Python36\python3.exe f:/python3/function. py------1---------a=1b=2C=defaultargs=() Kwargs= {}-------2---------a=1b=2C=3args=() Kwargs= {}--------3----------a=1b=2C=3args=() Kwargs= {'args': ('python','Java'),'name':'Devilf','Sxe':' Boy'}---------4------------a=1b=2C=3args= ('a','b','C') Kwargs= {'N1':'name','N2':'Sex','N3':' Age'}

As you can see, when combining parameters, each parameter needs to be corresponding, the parameters of the position, the default parameters, and so on.

3. Recursive function

The recursive function is to call itself.

For example:

def func (x):     if 1 :         return 1    Else :         return 1  = func (3) print (a)

This is where you call yourself in the function, and the execution process is probably the same:

This is probably the form of drawing a flowchart to understand the following:

4. High-order function

A variable can point to a function, a function can receive a variable, and a function may receive another function as a parameter, a function called a higher order function.

For example:

def func (x,y,f):     return f (x) += func (2,-3, ABS) print (a)

Here ABS belongs to a built-in function, Func is a custom function, that is, func this function takes the ABS as a built-in function as a parameter.

Python function basics

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