Python Self-study note function 1

Source: Internet
Author: User

Calling functions

Python has many built-in functions that can be viewed in its pipe network document: https://docs.python.org/2/library/functions.html

The Help information can also be viewed at the command line in the case where the function name is known. For example, in Python, the absolute value of the function abs, can be viewed through Help (ABS) information on ABS

When calling the function, if the number of arguments passed is incorrect, the TypeError error will be reported, and Python will clearly indicate that the method has several parameters and passed several parameters, such as: The ABS method parameters only one, but if used to pass two parameters, such as ABS (1, 2), you will get the following errors,

Typeerror:abs () takes exactly one argument (2 given)

And if the number of arguments is correct but the type is not correct, you will get typeerror error message, prompt the wrong data type, such as: to ABS () into the string, shaped like abs (' a '), you will get the following error,

Typeerror:bad operand type for abs (): ' Str '

The comparison function cmp (x, y) requires two parameters, X<y, returns -1;X=-Y, returns 0;x>y, returns 1

In addition, Python's commonly used built-in functions also have type conversion functions, which are basically directly represented by a function name with the same name as the data type, such as:

>>> Int (' 123 ') 123>>> Int (12.34) 12>>> float (' 12.34 ') 12.34>>> str (1.23) ' 1.23 ' >>> Unicode (+) U ' >>> bool (1) true>>> bool (") False

It is important to note that the function name is actually a reference to a function object, it is possible to assign a function name to a variable, which is equivalent to giving the function an alias (which reminds us of the naming rules in the database), such as:

>>> a = ABS # variable a points to ABS function >>> A (-1) # So you can also call the ABS function by a 1

Python Self-study note function 1

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.