. NET Programmer's Python basic tutorial learn----functions and exception handling [fifth day]

Source: Internet
Author: User
Tags integer division unsupported

Today's main records, the use of functions in Python, and exception handling.

   A. Function:

1. Creation and invocation of functions.

def Add (val1,val2):     return  val1+val2;print Add (1,2)

2. Define a function that returns a multi-parameter, the returned parameter is returned as a tuple, a single value can be obtained by the following table index, and the following is a function that gets the sum of the difference between two numbers and two numbers. Overall is good, in C # We generally use out to achieve, a number of parameters change back, but that is not good readability.

def getsubandsum (val1,val2):     return  val1-val2,val1+val2print getsubandsum (2,3)  #Answer: (2,3)print  #Answer (2)

3. Functions implement multiple parameters. The following example implements the parameters of any parameter and. Multiple parameters can be achieved by *. 1 * Number converted to a tuple, 2 * number converted to a dictionary, as for the 3 * number has not tried, think of no one should be so complicated data ah.

defSumlist (*param): Sumnumber=0 forValinchParam:sumnumber+=ValreturnSumnumberPrintSumlist (A) >>6defShowlist (*param):PrintParamshowlist (The)>> (A) showlist ()>>()defShowdic (* *param):Print(param) showdic (Name='Frank', age=23)>>{' Age': 23,'Name':'Frank'}

5. Recursive function, the point of recursion is the function of the internal call itself, the following to see through the return to seek n!

Note: When N<1 returns no value, it is none

def Fun (N):     if (N==1):         return 1    elif(n>1):        return n*fun (N-1)Print Fun (3)>>6print fun ( -1)>>none

   two. Exception handling of functions:

Since there are functions, there are certain functions that can make mistakes and how to catch exceptions. Python is implemented using the try....except.....finally.

1. Type of exception: "Please forgive my laziness.", for the actual development, the individual think it should be exception use more, in fact, we do not care about what is wrong, the following errors in the program should go through the code to avoid, or to customize the exception.

2. Try .... except use.

def Div (val1,val2):     Try :         return val1/val2    except  zerodivisionerror:        return  'Error'print Div (1, 0)>>error

3. Capture multiple exceptions and print out exceptions. All exceptions can be captured by exception

defDiv (val1,val2):Try:        returnval1/Val2except(Zerodivisionerror,typeerror), E:returnEPrintDiv (1, 0)PrintDiv (1,'2')>>integer Divisionormodulo by Zero>>unsupported operand type (s) for/:'int'  and 'Str'

4.finally in the use of exception capture, regardless of whether there is an exception to the finally code snippet will be executed, general purposes such as reading the file can be in the finally closed file stream, so that the file stream does not close, file crashes

defDiv (val1,val2):Try:        returnval1/Val2except(Zerodivisionerror,typeerror), E:returnEfinally:        Print("No matter whether there have error,i would excute")PrintDiv (1,'2')PrintDiv ()>>No matter whether there have error,i would excute>>unsupported operand type (s) for/:'int'  and 'Str'>>No matter whether there have error,i would excute>>1

5. The problem comes, finally since must be executed, then a method inside if the finally code block has a "return value", the try also has a "return value", then call the method to get what data it. Let's take a look at the code below.

Note: It is not clear why this design is returned to the finally.

defDiv (val1,val2):Try:        returnval1/Val2except(Zerodivisionerror,typeerror), E:returnEfinally:        return  'I am finally'PrintDiv (1,'2')PrintDiv ()>>i amfinally>>i amfinally

three. Summary:

This chapter records the creation and basic use of functions. And the use of exceptions. There is still much to be learned in the back, such as:

1. "How to throw an exception in Python, not found." NET can pass throw, after all, in the development of the exception encountered, sometimes we need to further encapsulate the exception to the upper code to throw "

2. "How do I customize exceptions, which are similar to C # inherited exception classes?" This has to wait for the next few days to learn the knowledge of Python abstraction.

3. "Lamba Learning, Fitter,map,reduce learning." You should use a dedicated blog to record your study notes later. "

    

  

    

  

. NET Programmer's Python basic tutorial learn----functions and exception handling [fifth day]

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.