Last updated on 2023-05-08 | Edit this page
- How can I extend the capabilities of Python?
- How can I use software that other people have written?
- How can I find out what that software does?
- Explain what software libraries are and why programmers create and use them.
- Write programs that import and use libraries from Python’s standard library.
- Find and read documentation for standard libraries interactively (in the interpreter) and online.
- A (software) library is a collection of files (called
modules) that contains functions for use by other programs.
- May also contain data values (e.g., numerical constants) and other things.
- Library’s contents are supposed to be related, but there’s no way to enforce that.
- The Python standard library is an extensive suite of modules that comes with Python itself.
- Many additional libraries are available from PyPI (the Python Package Index).
- We will see later how to write new libraries.
importto load a library module into a program’s memory.
- Then refer to things from the module as
- Python uses
.to mean “part of”.
- Python uses
string, one of the modules in the standard library:
import string print('The lower ascii letters are', string.ascii_lowercase) print(string.capwords('capitalise this sentence please.'))
The lower ascii letters are abcdefghijklmnopqrstuvwxyz Capitalise This Sentence Please.
- You have to refer to each item with the module’s name.
string.capwords(ascii_lowercase)won’t work: the reference to
ascii_lowercasedoesn’t somehow “inherit” the function’s reference to
- Works just like help for a function.
Help on module string: NAME string - A collection of string constants. MODULE REFERENCE https://docs.python.org/3.6/library/string The following documentation is automatically generated from the Python source files. It may be incomplete, incorrect or include features that are considered implementation detail and may vary between Python implementations. When in doubt, consult the module reference at the location listed above. DESCRIPTION Public module variables: whitespace -- a string containing all ASCII whitespace ascii_lowercase -- a string containing all ASCII lowercase letters ascii_uppercase -- a string containing all ASCII uppercase letters ascii_letters -- a string containing all ASCII letters digits -- a string containing all ASCII decimal digits hexdigits -- a string containing all ASCII hexadecimal digits octdigits -- a string containing all ASCII octal digits punctuation -- a string containing all ASCII punctuation characters printable -- a string containing all ASCII characters considered printable CLASSES builtins.object Formatter Template ⋮ ⋮ ⋮
from ... import ...to load only specific items from a library module.
- Then refer to them directly without library name as prefix.
from string import ascii_letters print('The ASCII letters are', ascii_letters)
The ASCII letters are abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
import ... as ...to give a library a short alias while importing it.
- Then refer to items in the library using that shortened name.
import string as s print(s.capwords('capitalise this sentence again please.'))
Capitalise This Sentence Again Please.
- Commonly used for libraries that are frequently used or have long
- E.g., The
pandaslibrary is often aliased as
- E.g., The
- But can make programs harder to understand, since readers must learn your program’s aliases.
help(os)we see that we’ve got
os.getcwd()which returns a string representing the current working directory.
Given the variables
day, how would you generate a date in the standard iso
= 2016 year = 10 month = 22day
- Which standard library module could help you?
- Which function would you select from that module?
- Try to write a program that uses the function.
The datetime module seems like it could help you.
You could use
date(year, month, date).isoformat() to
convert your date:
import datetime = datetime.date(year, month, day).isoformat() iso_date print(iso_date)
or more compactly:
import datetime print(datetime.date(year, month, day).isoformat())
Importing the os module (
import string as s = s.digits numbers print(numbers)
can be written as
import string = string.digits numbers print(numbers)
Since you just wrote the code and are familiar with it, you might actually find the first version easier to read. But when trying to read a huge piece of code written by someone else, or when getting back to your own huge piece of code after several months, non-abbreviated names are often easier, expect where there are clear abbreviation conventions.
Match the following print statements with the appropriate library calls
from string import digits A) import string B) import string as sC)
1. print(list(s.digits)) 2. print(list(digits)) 3. print(string.ascii_uppercase)
digits methods B3) Importing
provides methods such as
ascii_uppercase, but requires the
string. syntax. C1) Importing
string with the
from math import degrees, pi = degrees(pi / 2) angle print(angle)
Most likely you find this version easier to read since it’s less
dense. The main reason not to use this form of import is to avoid name
clashes. For instance, you wouldn’t import
degrees this way
if you also wanted to use the name
degrees for a variable
or function of your own. Or if you were to also import a function named
degrees from another library.
- The date object takes arguments in the order year, month, day, so 13 is an invalid value for month.
- You get an error of type “ValueError”, indicating that the object received an inappropriate argument value. The additional message “month must be in 1..12” makes it clearer what the problem is.
- Most of the power of a programming language is in its libraries.
- A program must import a library module in order to use it.
helpto learn about the contents of a library module.
- Import specific items from a library to shorten programs.
- Create an alias for a library when importing it to shorten programs.