The Python Standard Library | Python 3 Standard Library Tutorial With Examples. Here in this blog post Coding compiler sharing a Python 3 standard library tutorial for beginners. This Python tutorial is for beginners and intermediate learners who are looking to master in Python programming. Experienced Python programmers also can refer this tutorial to brush-up their Python 3 programming skills. Let’s start learning Python 3.
Python Standard Library Overview
Let’s start learning about Python standard library and it’s sub modules.
The Python Standard Library
|1. Operating System Interface||7. Internet access|
|2. File Wildcards||8. Date and Time|
|3. Command line parameters||9. Data Compression|
|4. Error Output Redirection||10. Performance Measurement|
|5. String Regular Matches||11. Quality Control|
|6. Mathematics||12. Swiss Army Knife|
1. Operating System Interface
The os module provides many functions that interact with the operating system:
>>> import os >>> os . getcwd () # Return the current working directory 'C:\\Python35' >>> os . chdir ( '/server/accesslogs' ) # Change current working directory >>> os . System ( 'mkdir today' ) # Run the command mkdir in the system shell 0
The built-in dir() and help() functions are useful when using large modules like os :
>>> import os >>> dir ( os ) <returns a list of all module functions> >>> help ( os ) <returns an extensive manual page created from the module's docstrings>
For everyday file and directory management tasks, the shutil module provides an easy-to-use, high-level interface:
>>> import shutil >>> shutil . copyfile ( 'data.db' , 'archive.db' ) 'archive.db' >>> shutil . move ( '/build/executables' , 'installdir' ) 'installdir'
2. File Wildcards
The glob module provides a function for generating a list of files from a directory wildcard search:
>>> import glob >>> glob . glob ( '*.py' ) ['primes.py', 'random.py', 'quote.py']
3. Command line parameters
Common tool scripts often call command line arguments. These command line arguments are stored as linked lists in the argv variable of the sys module . For example execute the command line you may get the following result:
python demo.py one two three
>>> import sys >>> print ( sys . argv ) ['demo.py', 'one', 'two', 'three']
The getopt module uses the Unix getopt() function to handle sys.argv . More complex command line processing is provided by the argparse module.
Related Article: Python For Machine Learning
4. Error Output Redirection and Program Termination
Sys also has stdin , stdout, and stderr attributes that can be used to display warning and error messages even when stdout is redirected:
>>> sys . stderr . write ( 'Warning, log file not found starting a new one \n ' ) Warning, log file not found starting a new one
Most of the script’s direct termination is used
Related Article: Python Data Structures
5. String Regular Matches
The re module provides regular expression tools for advanced string processing. For complex matching and processing, regular expressions provide a simple, optimized solution:
>>> import re >>> re . findall ( r '\bf[az]*' , 'which foot or hand fell fastest' ) ['foot', 'fell', 'fastest'] >>> re . sub ( r '(\b[az]+) \1' , r '\1' , 'cat in the hat' ) 'cat in the hat'
String methods are best used for simple operations because they are easy to read and easy to debug:
>>> 'tea for too' . replace ( 'too' , 'two' ) 'tea for two'
The math module provides access to the underlying C library for floating-point operations:
>>> import math >>> math . cos ( math . pi / 4.0 ) 0.70710678118654757 >>> math . log ( 1024 , 2 ) 10.0
Random provides a tool for generating random numbers:
>>> import random >>> random . choice ([ 'apple' , 'pear' , 'banana' ]) 'apple' >>> random . sample ( range ( 100 ), 10 ) # sampling without replacement [30, 83, 16, 4, 8, 81, 41, 50, 18, 33] >>> random . random () # random float 0.17970987693706186 >>> random .Randrange ( 6 ) # random integer chosen from range(6) 4
The SciPy < https://scipy.org > project provides many numerical calculation modules.
Related Article: Python Input and Output
7. Internet access
There are several modules for accessing the Internet and handling network communication protocols. The simplest of these two are urllib.request for handling data received from urls and smtplib for sending emails :
>>> from urllib.request import urlopen >>> for line in urlopen ( 'https://tycho.usno.navy.mil/cgi-bin/timer.pl' ): ... line = line . decode ( ' Utf-8' ) # Decoding the binary data to text. ... if 'EST' in line or 'EDT' in line : # look for Eastern Time ... print ( line ) <BR>Nov. 25, 09:43:32 PM EST >>> import smtplib >>> server = smtplib . SMTP ( 'localhost' ) >>> server . sendmail ( '[email protected]' , '[email protected]' , ... """To: jcaesar @example.org ... From: [email protected] ... ... Beware the Ides of March. ... """" ) >>> server . quit ()
(Note that the second example requires running a mail server on localhost.)
Related Article: Python Errors and Exceptions
8. Date and Time
The datetime module provides both simple and complex methods for date and time processing. While supporting date and time algorithms, the implementation focuses on more efficient processing and formatting of the output. The module also supports time zone processing.
>>> # dates are easily constructed and formatted >>> from datetime import date >>> now = date . today () >>> now datetime.date(2003, 12, 2) >>> now . strftime ( "% M- %d -%y. %d %b %Y is a %A on the %d day of %B." ) '12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.' >>> # a dates Support Calendar Arithmetic >>> Birthday = DATE ( 1964 , 7 , 31 ) >>> Age = now - Birthday >>> Age . Days 14368
9. Data Compression
The following modules directly support common data packaging and compression formats: zlib , gzip, bz2 , lzma , zipfile, and tarfile .
>>> import zlib >>> s = b 'witch which has which witches wrist watch' >>> len ( s ) 41 >>> t = zlib . compress ( s ) >>> len ( t ) 37 >>> Zlib . decompress ( t ) b'witch which has which witches wrist watch' >>> zlib . crc32 ( s ) 226805979
10. Performance Measurement
Some users are interested in understanding the performance differences between different approaches to the same problem. Python provides a measurement tool that provides direct answers to these questions.
For example, using tuples for encapsulation and unpacking to swap elements looks much more enticing than using traditional methods. Timeit proves that the latter is faster:
>>> from timeit import Timer >>> Timer ( 't=a; a=b; b=t' , 'a=1; b=2' ) . timeit () 0.57535828626024577 >>> Timer ( 'a,b = b,a' , 'a=1; b=2' ) . timeit () 0.54962537085770791
With respect to the fine- grainedness of timeit , the profile and pstats modules provide time measurement tools for larger code blocks.
11. Quality Control
One of the ways to develop high quality software is to develop test code for each function and often test it during development.
The doctest module provides a tool to scan the module and perform tests based on the docstrings embedded in the program. Testing the construct is like simply cutting and pasting its output into a docstring.
Through the user-provided example, it has developed a document that allows the doctest module to confirm that the results of the code are consistent with the document:
Def average ( values ): """Computes the arithmetic mean of a list of numbers. >>> print(average([20, 30, 70])) 40.0 """ return sum ( values ) / len ( values ) Import doctest doctest . testmod () # auto validate the embedded tests
The unittest module is not as easy to use as the doctest module, but it can provide a more comprehensive set of tests in a separate file:
Import unittest Class TestStatisticalFunctions ( unittest . TestCase ): Def test_average ( self ): self . assertEqual ( average ([ 20 , 30 , 70 ]), 40.0 ) self . assertEqual ( round ( average ([ 1 , 5 , 7 ]), 1 ), 4.3 ) with self . assertRaises ( ZeroDivisionError ): average () with self . assertRaises (TypeError ): average ( 20 , 30 , 70 ) Unittest . main () # Calling from the command line invokes all tests
12. Swiss Army Knife
Python presents the philosophy of the “Swiss Army Knife”. This can be best demonstrated by the advanced and robust features of its larger package. Columns such as:
The xmlrpc.client and xmlrpc.server modules make remote procedure calls a breeze. Although the module has such a name, users do not need to have knowledge of XML or process XML.
The email package is a library for managing email messages, including MIME and other RFC2822 based information files.
Unlike the smtplib and poplib modules that actually send and receive information , the email package contains a complete set of tools for constructing or parsing complex message structures (including attachments) and implementing Internet encoding and header protocols.
The xml.dom and xml.sax packages provide powerful support for popular information exchange formats. Similarly, the csv module supports direct read and write in the common database format.
Taken together, these modules and packages greatly simplify data exchange between Python applications and other tools.
Internationalization is supported by the gettext , locale, and codecs packages.