Libraries and Packages
Standard library
The Python standard library comes with a range of functionality for handling tasks. The following XKCD comic illustrates the extent of the existing functionality rather well (source: https://xkcd.com/353/).

The standard library has a collection of modules that can be imported into the program. As an example, if one would would wish to work with mathematical functions, the math module could be a good choice.
Modules are imported using the import
keyword, which is followed by the name of the module. The following outlines how to import the math
module, using the sqrt
function from the module to calculate the square root of a number.
The following on the other hand outlines how to create random numbers using the random module. The random
module has a function called randint
that returns a random integer between the given numbers (inclusive).
External packages
In addition to the standard library, Python has a range of third-party software that can be used to perform tasks. The third-party software are installed as packages, which are bundles containing relevant code. One of the main sources for third-party packages is the Python Package Index (PyPI).
The packages are installed using the pip
command, which is a package manager for Python.
The Python documentation has a good tutorial about installing packages, available at https://packaging.python.org/en/latest/tutorials/installing-packages/. The guide includes also discussion about creating virtual environments that can be used to constrain specific packages to a specific project.
As an example, to install the requests library, which is a library for creating HTTP requests, we would run the command pip install requests
(or a variant of it, depending on the operating system — see the above link to the guide about installing packages).
When working in Jupyter Notebook, the command can be run using the !
character as !pip install requests
.
The requests library provides basic HTTP functionality. It has a get
method that takes a URL as an argument and returns a response object. The response object has a status_code
attribute that returns the status code of the request and a text
attribute that contains the response body as a string.
The use of the library is demonstrated below — as the API, we use the simple joke api at https://simple-joke-api.deno.dev/random that returns random jokes.
import requests
response = requests.get("https://simple-joke-api.deno.dev/random")
print(response.status_code)
print(response.text)
Running the above program, one possible output is as follows.
200
{"setup":"What is the most used language in programming?","punchline":"Profanity."}
If you see an error “ModuleNotFoundError: No module named ‘requests’”, it means that the requests
library is not available in the Python environment. To install the library, run the command pip install requests
in the terminal.
The response from the API is a JSON string. To accommodate for this, we can use the json
method to convert the response to a Python dictionary.
import requests
response = requests.get("https://simple-joke-api.deno.dev/random")
print(response.status_code)
print(response.text)
response_dict = response.json()
print(response_dict["setup"])
print(response_dict["punchline"])
200
{"setup":"I told a programmer to come back after a while..","punchline":"But they never returned."}
I told a programmer to come back after a while..
But they never returned.