What is a function of Python

Source: Internet
Author: User
Tags abs

We know that the area of the circle is calculated as:

S =ΠR2

When we know the value of radius r, we can calculate the area based on the formula. Suppose we need to calculate the area of 3 circles of different sizes:

R1 = 12.34R2 = 9.08r3 = 73.1S1 = 3.14 * R1 * r1s2 = 3.14 * R2 * r2s3 = 3.14 * R3 * R3

When the code is repeating regularly, you need to be careful, writing 3.14 * x * x is not only troublesome, but if you want to change 3.14 to 3.14159265359, you have to replace it all.

With the function, we will not write S = 3.14 * x * x every time, but rather write a more meaningful function call s = area_of_circle(x) , and the function area_of_circle itself only needs to be written once, it can be called multiple times.

Abstraction is a very common concept in mathematics. As an example:

Calculate the sum of the series, such as:1 + 2 + 3 + ... +, writing is very inconvenient, so mathematicians invented the summation symbol ∑, can be 1 + 2 + 3 + ... + 100:

∑nn=1 

This abstract notation is very powerful, because we see that ∑ can be understood as sums, rather than reduced to lower-level addition operations.

Moreover, this abstract notation is extensible, such as:

∑(n2+1) n=1 

The reduction into the addition operation becomes:

(1 x 1 + 1) + (2 x 2 + 1) + (3 x 3 + 1) + ... + (x 100 + 1)

It can be seen that, with the help of abstraction, we do not care about the underlying computational process, but directly consider the problem at a higher level.

The same is true of computer programs, where functions are the most basic form of code abstraction.

Python is not only very flexible in defining functions, but it has built in many useful functions that can be called directly.

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 an absolute function abs, which receives a parameter.

Documents can be viewed directly from the official website of Python: http://docs.python.org/2/library/functions.html#abs

You can also view the Help information for the ABS function on the interactive command line by helping (ABS).

Call the abs function:

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

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

>>> ABS (1, 2) Traceback (most recent):  File "<stdin>", line 1, in <module>typeerror:ab S () 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 '

The 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

Python's built-in common functions also include data type conversion functions, such as the int () function, which converts other data types to integers:

>>> Int (' 123 ') 123>>> Int (12.34) 12

The STR () function converts other types to str:

>>> STR (123) ' 123 ' >>> str (1.23) ' 1.23 '

The sum () function takes a list as a parameter and returns the sum of all the elements of the list. Please calculate 1*1 + 2*2 + 3*3 + ... + 100*100
L = Range (1, 101)
Print sum ([i**2 for I in L])


What is a function of Python

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.