1, the module
1.1 is a program module
To tell the interpreter where to find module executable:
Created after import module __pycache__ subdirectories that contain files processed.
Module 1.2 is used to define the
Classes and functions defined in a module and subjected to the variable assignment will become properties of the module.
__Name__ set the variable value can be checked or module is introduced as a program to run another program.
1.3 allow modules available
1.3.1 the program in the right place
Similar to the placement module on the local site-packages, so that all programs can be introduced into it
1.3.2 interpreter told where to find
Standard practice is to the directory where the module is included in the PYTHONPATH environment variable.
1.4 package
In fact, the package is another module, which may contain other modules.
To be regarded as Python package, the directory must contain a file __init__.py , the module is stored in a file with the extension .py in.
When you call the package name, the package content is stored in the __init__.py.
2, to explore module
What module contains 2.1
1, using dir
The dir function, the object lists all the properties (for modules, lists all of the functions, classes, variables, etc.).
2, variable __all__
2.2 Use help to get help
2.3 Documents
2.4 Source Code
3, the standard library: Some popular module
3.1 sys
Sys module can access variables and functions closely related to the python interpreter
3.2 the
Os module can access multiple operating system services.
3.3 fileinput
Fileinput module can easily iterate over all rows a series of text files.
3.4 collection, and heap deque
1, a collection of
Variable is set, containing only immutable (hash) value.
frozenset type , which represents immutable (available hash) collection.
2, the heap
Heap priority queue is a data structure. Priority Queuing objects can be added in any order, at any time and find out (and delete) the smallest element (may be added between the two objects).
Python no separate stack type, and only one module comprising a number of stack operation heapq function (where q represents the queue).
heapq contains six functions, the first four of which is directly associated with reactor operation.
You must use the list to represent the heap object itself.
Characterized heap (heap property): the element at position i @ 2 is always greater than the element at the position i (Conversely element position is less than 2 * i and * at + i 1 2.
function | description |
heappush(heap,x) | The pile press-x |
heappop(heap) | Pop the smallest element from the heap |
heapify(heap) | Make a list of characteristics with heap |
heapreplace(heap,x) | Pop smallest element, is pressed into the stack and x |
nlargest (n trip) | Iter returns the largest element in the n |
nsmallest(n,iter) | Iter returns the smallest element of n |
3, deque
Deque support (left end) of additional efficient and pop elements in the first team, but do not use the list.
Deque iterator created from a subject.
Collections of modules, comprising a set, and several other types deque (Collection) type.
Other methods: popleft, appendleft, extendleft.
Iterables extendleft used in the elements will be the reverse order found in the deque.
3.5 time
Module comprises means for obtaining a current time, an operation time and date, the date is read from a string, formatted as a function of the date string.
function | description |
astime([tuple]) | The conversion time is a string tuple |
localtime([secs]) | The number of seconds into presentation date tuples local time |
mktime(tuple) | The conversion time is local time tuple |
sleep(secs) | Sleep secs seconds |
strptime(string[,format]) | The time string into tuple |
time() | current time |
datatime provides the date and time arithmetic support.
timeit can help calculate the execution time code segment.
3.6 random
random() | Returns a random real number of 0-1 |
getrandbits (a) | Integer n in a random manner to return the bins |
uniform(a,b) | Ab & returns (including) a random real number |
randrange ([start], Stipe, [step]) | Selecting a number from the range (strat, stop, step) at random |
choice(seq) | Select an element from a sequence randomly seq |
shuffle(seq[,random]) | Place disrupt the sequence seq |
sample(seq,n) | Selecting n different values from a sequence of random elements in seq |