Home >Backend Development >Python Tutorial >How to call built-in functions in python? (Example analysis)

How to call built-in functions in python? (Example analysis)

乌拉乌拉~
乌拉乌拉~Original
2018-08-21 18:01:162473browse

For those who are exposed to the python programming language for the first time, when they first started learning python programming, they had relatively little understanding of python function calling. In this article Let's learn how python calls functions.

Python has many useful built-in functions that we can call directly.

To call a function, you need to know the name and parameters of the function. For example, the function abs, which finds the absolute value, has only one parameter.

If you want to know the specific built-in functions, you can view the documentation directly from Python’s official website:

http://docs.python.org/3/library/functions. html#abs

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

Calling the abs function:

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

When calling the function, if the number of parameters passed in is incorrect, a TypeError will be reported, and Python will clearly tell you: abs() has and only There is 1 parameter, but two are given:

>>> 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 parameters passed in is correct, but the parameter type cannot be accepted by the function, a TypeError error will be reported and an error message will be given. : str is the wrong parameter type:

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

And the max function max() can receive any number of parameters and return the largest one:

>>> max(1, 2)
2
>>> max(2, 3, 1, -5)
3

The above is all described in this article Content, this article mainly introduces the relevant knowledge of calling functions in python. I hope you can use the information to understand the above content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.

For more related knowledge, please visit the

Python tutorial column on the php Chinese website.

The above is the detailed content of How to call built-in functions in python? (Example analysis). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn