This paper takes the numerical calculation as an example to explain the use of functions in Python and share them for reference. Specifically as follows:
We all know that the area of the circle is calculated by:
S =ΠR2
When we know the value of radius r, we can calculate the area according to the formula. Let's say we need to calculate the size of 3 circles of different sizes:
R1 = 12.34
r2 = 9.08 R3 = 73.1 S1 = 3.14 * R1 * R1 s2 = 3.14 * R2 * R2
s3 = 3.14 * R3 * * R3
When the code is regularly repeated, you need to be careful, write 3.14 * x x is not only very troublesome, and if you want to change 3.14 to 3.14159265359, you have to replace it all.
With a function, we no longer write the S = 3.14 * x x each time, but rather write a more meaningful function call S = area_of_circle (x), and the function area_of_circle itself can be invoked multiple times only once.
Basically all the high-level languages support functions, and Python is no exception. Python not only has the flexibility to define functions, but it also has built-in many useful functions that can be invoked directly.
Abstract
Abstraction is a very common concept in mathematics. As an example:
Calculation of the sum of the series, such as: 1 + 2 + 3 + ... + 100, writing is very inconvenient, so mathematicians invented the summation symbol ∑, can be 1 + 2 + 3 + ... + 100:
This kind of abstract notation is very powerful, because we see ∑ can be understood as summation, rather than reduced to lower-level addition operations.
Moreover, this abstract notation is extensible, such as:
The reduction into the addition operation becomes:
(1 x 1 + 1) + (2 x 2 + 1) + (3 x 3 + 1) + ... + (x 100 + 1)
Visible, with the aid of abstraction, we can not care about the bottom of the specific calculation process, and directly at a higher level of thinking.
Writing a computer program is the same as a function is the most basic way of code abstraction. Flexible application helps to improve programming efficiency.
I hope the examples described in this article will help you with Python programming.