Python's common modules

Source: Internet
Author: User

Understanding Modules

What is a module?

Common scenario: A module is a file that contains Python definitions and declarations, and the file name is the suffix of the module name plus the. Py.

In fact, the import loaded module is divided into four general categories:

1 code written using Python (. py file)

2 C or C + + extensions that have been compiled as shared libraries or DLLs

3 packages for a set of modules

4 built-in modules written and linked to the Python interpreter using C

Why use a module?

If you quit the Python interpreter and then re-enter, then the functions or variables you defined previously will be lost, so we usually write the program to a file so that it can be persisted and executed in Python test.py when needed, when test.py is called a scripting script.

With the development of the program, more and more functions, in order to facilitate management, we usually divide the program into a file, so that the structure of the program is clearer and easier to manage. At this point, we can not only use these files as scripts to execute, but also as a module to import into other modules, to achieve the reuse of functionality.

Import and use of modules

The module should be imported at the beginning of the program.

Common Modules Collections Module

On the basis of the built-in data type (DICT, list, set, tuple),The collections module also provides several additional data types: Counter, deque, Defaultdict, Namedtuple, and Ordereddict.

1.namedtuple: Generate a tuple that can access the content of an element by using a name

2.deque: Double-ended queue to quickly append and eject objects from the other side

3.Counter: Counter, mainly used to count

4.OrderedDict: Ordered Dictionary

5.defaultdict: Dictionary with default values

Namedtuple

We know that we tuple can represent the invariant set, for example, a two-dimensional coordinate of a point can be expressed as:

>>> p = (1, 2)

However, it is difficult to see (1, 2) that this tuple is used to represent a coordinate.

At this point, namedtuple It comes in handy:

 from Import namedtuple>>> point = Namedtuple ('point', ['x'  'y'])>>> p = Point (1, 2)>>> p.x 1>>> p.y2

Class , if you want to represent a circle with coordinates and radii, you can also use the namedtuple definition:

# namedtuple (' name ', [Property list]): Circle = namedtuple ('circle', ['x'y  "R"])

Python's common modules

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.