Using Libraries in Python

You need the value of π or the square root of a number. Deriving the formula and checking it yourself is slow and easy to get wrong, yet someone has already written and debugged that code for you. Ready-made sets of such code are called libraries: you plug the one you need in with a single line and use it.

What are libraries?

Importing libraries

To use a library, you first need to import it into your program. Python offers several ways to import:

Importing the entire library

We plug in the whole module. Everything inside it stays behind the module's name, and we reach it through a dot:

Python 3.13
import math

radius = 5
circle_area = math.pi * radius ** 2
print(f"The area of a circle with radius {radius} is {circle_area:.2f}")
The area of a circle with radius 5 is 78.54

Importing specific elements

We take only what we need out of the module. Those names land directly in your program, so there is no math. to type in front of them any more:

Python 3.13
from math import sqrt, floor

x = 16
result = sqrt(x)
print(f"The square root of {x} is {result}")
The square root of 16 is 4.0
y = floor(3.7)
print(f"Floor of 3.7 is {y}")
Floor of 3.7 is 3

Importing with renaming

The same whole-module import, but under a short name. Handy when the full name is long or already taken by a variable of yours:

Python 3.13
import math as m

angle = 45
sin_value = m.sin(m.radians(angle))
print(f"The sine of {angle} degrees is {sin_value:.4f}")
The sine of 45 degrees is 0.7071

Types of libraries in Python

In Python, there are three main types of libraries:

  1. Built-in modules — modules that are already included in the Python standard library and are available immediately after installing Python.

  2. Third-party libraries — modules created by other developers that need to be installed additionally.

  3. Custom modules — modules that you create yourself to organize your code.

Examples of built-in modules

Python 3.13
import random

random_number = random.randint(1, 10)
print(f"Random number: {random_number}")
Random number: 7
import datetime

current_date = datetime.datetime.now()
print(f"Current date and time: {current_date}")
Current date and time: 2023-07-15 14:30:45.123456

Finding functions in the documentation

When working with libraries, it's important to know how to find information about available functions. Python provides several ways:

Using the help() function

help() prints everything the module says about itself: what it is for, the list of functions, a description of each. For math that help is long, so what you see below is only its beginning, and the ellipsis at the end means it does not stop there.

Python 3.13
import math
help(math)
Help on module math:

NAME
    math

MODULE REFERENCE
    https://docs.python.org/3.12/library/math.html

    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
    This module provides access to the mathematical functions
    defined by the C standard.

FUNCTIONS
    acos(x, /)
        Return the arc cosine (measured in radians) of x.

        The result is between 0 and pi.
...

Using the dir() function

Python 3.13
import random
attributes = dir(random)

# Let's display only the first 10 elements for brevity
print(attributes[:10])
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_ONE', '_Sequence']

Understanding Check

Which of the following library import methods are correct in Python?


In the next lesson we'll look at built-in libraries — the ones available right after you install Python.