# Example of function usage in Python

Source: Internet
Author: User
This article mainly introduces the usage of functions in Python, and describes the abstract features of functions in Python program design using numerical calculation examples, for more information about functions in Python, see this document. The details are as follows:

We all know that the area Calculation Formula of the circle is:

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 three 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 repeats regularly, you need to be careful. writing 3.14 x every time is not only troublesome, but if you want to change 3.14 to 3.14159265359, replace all.

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

Basically, all advanced languages support functions, and Python is no exception. Python not only flexibly defines functions, but also has many built-in useful functions that can be called directly.

Abstraction

Abstract is a very common concept in mathematics. For example:

Calculate the sum of the series, such as: 1 + 2 + 3 +... + 100, it is very inconvenient to write, so mathematicians invented the summation symbol Σ, which can put 1 + 2 + 3 +... + 100 note:

`100∑nn=1`

This abstract notation is very powerful, because we can see that Σ can be understood as summation, rather than being restored to low-level addition operations.

In addition, this abstract notation is extensible, for example:

`100∑(n2+1)n=1`

After being restored to an addition operation, it becomes:

(1x1 + 1) + (2x2 + 1) + (3x3 + 1) +... + (100x100 + 1)

It can be seen that with abstraction, we can ignore the specific computing process at the underlying layer and directly think about the problem at a higher level.

The same is true for writing computer programs. functions are the most basic method of code abstraction. Flexible application helps improve programming efficiency.

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

Related Keywords:

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.