[Learn Python step by step] 11. Common standard Libraries

Source: Internet
Author: User

After installing Python, we have also obtained a powerful Python standard library, which can save us a lot of time. Here is a simple description of some common standard libraries. For more information about the standard library, see the Python documentation.

Sys module

The sys module can access the variables and functions closely related to the Python interpreter. below issysSome common functions and variables in the module:

-

 

The following example usesargvObtain the command line parameters and reverse them:

= sys.argv[1:]   .join(args)

For more instructions, see the Python documentation: http://docs.python.org/2/library/sys.html

 

OS Module

The OS module provides many features to access operating system services. The following are some common functions and variables:

,,

 

The following example usesenvironVariable to querypathVariable value:

 os.environ[]

 

Path delimiters returned by different operating systems:

 os.sep 

For more instructions, see the Python documentation: http://docs.python.org/2/library/ OS .html

 

Fileinput Module

fileinputThe module can easily traverse all rows of a text file. Below isfileinputImportant functions in the module:

 

For more instructions, see the Python documentation: http://docs.python.org/2/library/fileinput.html

 

Set

Set is introduced in Python 2.3. The Set class is located insetsModule. You can directly use the collection without importing it:

 set(range(10

 

A set is constructed by sequences (or other iteratable objects). They are mainly used to check membership, so copies are ignored:

 set([0, 1, 2, 3, 1, 2, 3

 

In addition to checking membership, you can also use standard set operations, such as Union and intersection. You can either use methods or directly use operators:

 a = set([1,2,3 b = set([2,3,4,5  a.union(b)   a | b   c = a &  c.issubset(a)    c <= a   c.intersection(b)    a & b   a.difference(b)    a - b   a.symmetric_difference(b)    a ^ b   a.copy()   a.copy()  a 

For more use of operators and methods, see the Python documentation: http://docs.python.org/2/library/sets.html

 

A set is variable and can only contain unchangeable values. However, a set containing a set is common. In this case, we only need to usefrozensetType to wrap the set,frozensetThe constructor can create a copy of a given set:

a = set([1,2,3= set([2,3,4,5 a 

 

Heap

Heap is a type of priority queue. The priority queue can be used to add objects in any order, and can be found (or removed) at any time (while adding objects) the smallest element (more efficient than the min method in the list ). There is no independent heap type in Python-only one module that contains some heap operations. This module isheapqContains six functions:

 

 

heappushThe function is used to add heap items, as shown below:

  heapq  *  random   data = range(10  heap =  n     heap   heappush(heap,0.5  heap 

For more detailed usage and documentation, see the Python documentation: http://docs.python.org/2/library/heapq.html

 

Heap property)

The sorting of heap elements is regular: The elements at the I position are always larger than those at the I/2 position (or the total ratio of elements at the I position is 2 ).I and 2The element at the position 1 + 1 is small)

 

Dual-end queue

Double-ended queue is useful when elements need to be removed in the order they are added. A dual-end queue is created through an iteratable object (such as a set:

  collections   q = deque(range(5 q.append(5 q.appendleft(6   q   q.pop()   q.rotate(3  q   q.rotate(-1  q 

For more detailed usage and documentation, see the Python documentation: http://docs.python.org/2/library/collections.html#collections.deque

 

Time Module

The time module provides the following functions: Get the current time, operation time and date, read time from the string, and format the time string. Real numbers can be used for dates (the number of seconds since, January 1, a platform-related year for UNIX ), or the tuples that contain nine integers indicate the following meanings:

(2008,1,21,12,2,56,0,21,0) 

 

The following are the most common functions of the time module:

 

 

For more detailed usage and documentation, see the Python documentation: http://docs.python.org/2.7/library/time.html

In addition to the time module, Python also provides two time-related modules:

  • The datetime module () supports Date and Time algorithms.
  • Timeit module calculates the code segment execution time

 

Random Module

The random module contains a function that returns a random number (pseudo-random number). below israndomImportant Functions of the module:

Random () returns a random real number n between 0 and ≤ n <1, where 0 <n ≤ 1 <

 

In the following example, 2008 ~ A random day between 2009:

 random  *  time  *= (2008,1,1,0,0,0,-1,-1,-1== (2009,1,1,0,0,0,-1,-1,-1== asctime(localtime(random_time))

For more detailed usage, see the Python documentation: http://docs.python.org/2/library/random.html

 

Shelve Module

UseshelveThe module provides a simple file storage solution. We can persist an object to a file as follows:

        data = shelve.open(     employee =     employee[] =      employee[] =      pid =               data[pid] =          == : main()

 

After persistence, you can read the content in the file again:

= = shelve.open( data[pid]   == : main()

For more detailed usage, see the Python documentation: http://docs.python.org/2/library/shelve.html

 

References & further reading

Basic Python tutorial (version 2)

Python Document

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.