Python Call function

Source: Internet
Author: User

Python has built in a lot of useful functions that we can call directly.

To invoke a function, you need to know the name and parameters of the function, such as a function that asks for an absolute value abs , and only one argument. Documents can be viewed directly from the official Python website:

Http://docs.python.org/2/library/functions.html#abs

You can also help(abs) view the abs function's help information on the interactive command line.

Call abs function:

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

When calling a function, if the number of arguments passed in is incorrect, the error will be reported TypeError , and Python will explicitly tell you that ABS () has only 1 parameters, but gives two:

>>> 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 arguments passed in is correct, but the parameter type cannot be accepted by the function, TypeError the 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‘

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

>>> cmp(1, 2)-1>>> cmp(2, 1)1>>> cmp(3, 3)0
Data type conversions

Python's built-in common functions also include data type conversion functions, such as int() functions that 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, and it is possible to assign a function name to a variable, which is equivalent to giving the function an "alias":

>>> a = abs # 变量a指向abs函数>>> a(-1) # 所以也可以通过a调用abs函数1
Summary

To invoke a Python function, you need to pass in the correct parameters according to the function definition. If the function call error, must learn to read the error message, so English is very important!

Python Call function

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.