Python the third day

Source: Internet
Author: User
Tags pow

Function

1). Calling functions

A) call a function to know the function name and parameters, through the Help (name) can see the functions of the group information, if the function is called when the number of arguments passed or the wrong type of the parameter, will be reported typeerror error, but the specific information is different

b) A data type conversion function int (), converted to an integer str () converted to a string float (), converted to a floating-point number bool () to convert to Boolean data true False

c) Function name is actually a reference to a function, it is possible to assign a function name to a variable, which is equivalent to an alias to the function

2) Defining functions

A) function definition, to invoke function must first import function, from path/file name Import function name from CLU2 import My_abs

    def  my_abs (x):      if x>=0:          return  x        else:          return-x    //return indicates that none is returned  

b) empty function knowledge to define a function, do nothing, you can pass the PASS statement

Def pop ():

Pass

c) Improve the custom take absolute value function, so that when the function is called to pass the wrong type parameter, there will be a corresponding message (only allow incoming integer or floating-point type)

Isinstance (obj,class_or_tuple) This function is mainly used to determine whether a data is a collection of the specified type, if it is true otherwise false

D) The function in Python returns multiple values (in fact, returns a tuple or a value)

function returns multiple values

In fact, a tuple is returned,

3) Function parameters

A) How many default parameters, variable parameters, keyword parameters, positional parameters are available for function parameters

positional parameters : When the function is called, the values passed in by the POW (2,4) are assigned to the parameters x and y in the position order

default parameter: when calling a function, you can not pass in the value (POW (2)) and the parameter will have a default value. This is useful for changing previous old code (just add a default parameter).

Default parameter usage error? Define a default parameter as follows

Call the above function, if the call with parameters is not a problem, if not with a function such as add_end (), Add_end (), the second call will output two [' End ', ' end ']

What is this for? In fact, because the list is a Mutable object, the value of s has changed since it was called once. Again the call will certainly change on the original basis,

Workaround: Initialize the S default value to an immutable object, such as S=none

Variable parameters????? Todo

Python the third 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.