Python function usage example tutorial, python usage example tutorial

Source: Internet
Author: User

Python function usage example tutorial, python usage example tutorial

This article uses numeric computing as an example to describe how to use functions in Python and share it with you for your reference. 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.


How to use functions in the Python Module

1. Know what function (func) you want, in that module, and then import xxx

2. help (xxx. func)

How do I use the delete function in python? It is best to include an example,

I want to correct the statement in the recommendation answer!
Del is not used to delete the memory space pointed to by reference. del in python and delete in c ++ are completely two concepts ..

In general, del is used to delete the reference of a variable, for example, a = 1; del a. Here a is a reference to the value of 1 (all variables in python can be considered as references ), del a deletes the reference relationship, that is, 1 is no longer referenced by the variable a, and the variable name a is also removed from the variable table.

If you still don't know, you will understand the following example:
A = object ()
B =
Del
Print B
Print a # This statement will report an undefined exception of.
In this Code, a references a new object (), and B = a makes B reference this object. Although a and B are two variables, however, they reference the same object, which is similar to the two pointers in c ++ pointing to the same object.
Del a only deletes the reference of the variable a to object (), and the variable name of a is invalid, but it does not mean that the object () is deleted, it is still there, and B still references it. This can be seen from the print B output below.

The memory release of python uses the reference counting mechanism, that is, when an object does not have any variables that reference it, it will be automatically released without manual intervention.

In addition, del has different functions for different objects, depending on the implementation of the _ del _ System Method of the object itself. For example, if List a = [1, 2, 3], del a [0] indicates that the first item of the List is deleted, and then a is changed to [2, 3. If it is a custom object, the del function can be defined as anything you want. For details, see the _ del _ entry in the python help.

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.