Examples of the use of Python built-in functions tutorial _python

Source: Internet
Author: User
Tags abs function definition stdin

This article briefly analyzes the use of built-in functions commonly used in Python, and share them for everyone's reference. The specific analysis is as follows:

In general, there are a number of useful functions built into python that we can call directly.

To call a function, you need to know the name and parameters of the function, such as the function of the absolute value of ABS, only one parameter. You can view the document directly from the official Python website: http://docs.python.org/2/library/functions.html#abs

You can also view Help for the ABS function on the interactive command line through the aid (ABS).

Call ABS function:

>>> ABS
>>> ABS ( -20)
>>> abs (12.34)
12.34

When calling a function, if the number of incoming arguments is incorrect, the TypeError error is reported, and Python tells you explicitly that the ABS () has 1 parameters but gives two:

>>> ABS (1, 2)
Traceback (most recent):
 File "<stdin>", line 1, in <module>
Ty Peerror:abs () takes exactly one argument (2 given)

If the number of arguments passed in is correct, but the parameter type cannot be accepted by the function, the TypeError error is reported and an error message is given: STR is the wrong parameter type:

>>> ABS (' a ')
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
Typeerror:bad operand type for abs (): ' Str '

and comparison function cmp (x, y) requires two parameters, if x<y, returns-1, if x==y, returns 0, if X>y, returns 1:

>>> CMP (1, 2)
-1
>>> CMP (2, 1)
1
>>> cmp (3, 3)
0

Data type conversions

The common functions built into Python include data type conversion functions, such as the Int () function to convert other data types to integers:

>>> Int (' 123 ')
123
>>> Int (12.34)
>>> float (' 12.34 ')
12.34
>>> str (1.23)
' 1.23 '
>>> Unicode (MB)
u ' m '
>>> bool (1)
True
>>> bool (')
False

A function name is actually a reference to a function object, and it is possible to assign a function name to a variable, which is equivalent to an "alias" to the function:

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

Summary:

Calling the Python function requires passing in the correct argument based on the function definition. If the function call is wrong, be sure to learn to read the error message, so English is very important!

I hope the examples described in this article will help you with Python programming.

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.