Python built-in function usage example tutorial, python built-in instance tutorial

Source: Internet
Author: User

Python built-in function usage example tutorial, python built-in instance tutorial

This article briefly analyzes the usage of built-in functions commonly used in Python and shares them with you for your reference. The specific analysis is as follows:

Generally, many useful functions are built in Python, which can be called directly.

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

You can also view the help information of the abs function through help (abs) in the interactive command line.

Call the abs function:

>>> abs(100)100>>> abs(-20)20>>> abs(12.34)12.34

When calling a function, if the number of input parameters is incorrect, A TypeError error is reported, and Python will clearly tell you that abs () has only one parameter, but two parameters are provided:

>>> abs(1, 2)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: abs() takes exactly one argument (2 given)

If the number of input parameters is correct, but the parameter type cannot be accepted by the function, a TypeError error is also reported, and the error message: str is the parameter type of the error:

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

The comparison function cmp (x, y) requires two parameters. If x <y,-1 is returned. If x = y, 0 is returned. If x> y, 1 is returned:

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

Data Type Conversion

Common built-in functions of Python also include data type conversion functions. For example, the int () function can convert other data types to integers:

>>> int('123')123>>> int(12.34)12>>> float('12.34')12.34>>> str(1.23)'1.23'>>> unicode(100)u'100'>>> bool(1)True>>> bool('')False

The function name is actually a reference to a function object. You can assign the function name to a variable, which is equivalent to an alias for the function ":

>>> A = abs # variable a points to abs function >>> a (-1) # so you can call abs function 1 through.

Summary:

To call a Python function, you must pass in the correct parameters according to the function definition. If a function call error occurs, you must read the error message. Therefore, it is important to use English!

I hope the examples described in this article will be helpful for Python program design.


Who will give an example of several python built-in functions?

All built-in functions of python

Abs () divmod () input () open () staticmethod ()
All () enumerate () int () ord () str ()
Any () eval () isinstance () pow () sum ()
Basestring () execfile () issubclass () print () super ()
Bin () file () iter () property () tuple ()
Bool () filter () len () range () type ()
Bytearray () float () list () raw_input () unichr ()
Callable () format () locals () reduce () unicode ()
Chr () frozenset () long () reload () vars ()
Classmethod () getattr () map () repr () xrange ()
Cmp () globals () max () reversed () zip ()
Compile () hasattr () memoryview () round () _ import __()
Complex () hash () min () set () apply ()
Delattr () help () next () setattr () buffer ()
Dict () hex () object () slice () coerce ()
Dir () id () oct () sorted () intern ()

I have read it all over again. What kind of usage do you want to know?

Can a value assignment in python be replaced by a built-in function?

What does LZ want to do? You can write a function to complete the assignment. If you do not like the assignment ....
 

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.