Examples of usages of functions in Python tutorial _python

Source: Internet
Author: User

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:


∑n
N=1

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:


∑ (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)

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.

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.