Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

Learning objectives

By the end of this section you should be able to

  • Explain differences between the standard library and PyPI.
  • Search python.org and pypi.org for modules of interest.

Built-in modules

The Python Standard Library is a collection of built-in functions and modules that support common programming tasks. Ex: The math module provides functions like sqrt() and constants like pi. Python's official documentation includes a library reference and a module index for becoming familiar with the standard library.

For decades, Python has maintained a "batteries included" philosophy. This philosophy means that the standard library should come with everything most programmers need. In fact, the standard library includes over 200 built-in modules!

Module Description

calendar

General calendar-related functions.

datetime

Basic date and time types and functions.

email

Generate and process email messages.

math

Mathematical functions and constants.

os

Interact with the operating system.

random

Generate pseudo-random numbers.

statistics

Mathematical statistics functions.

sys

System-specific parameters and functions.

turtle

Educational framework for simple graphics.

zipfile

Read and write ZIP-format archive files.
Table 7.1 Example built-in modules in the standard library.

Concepts in Practice

Built-in modules

Use the library reference, module index, and documentation links above to answer the questions.

1.
Which page provides a list of built-in modules sorted by category?
  1. library reference
  2. module index
  3. PEP 2
2.
What is the value of calendar.SUNDAY?
  1. 1
  2. 6
  3. 7
3.
Which built-in module enables the development of graphical user interfaces?
  1. tkinter
  2. turtle
  3. webbrowser

Third-party modules

The Python Package Index (PyPI), available at pypi.org, is the official third-party software library for Python. The abbreviation "PyPI" is pronounced like pie pea eye (in contrast to PyPy, a different project).

PyPI allows anyone to develop and share modules with the Python community. Module authors include individuals, large companies, and non-profit organizations. PyPI helps programmers install modules and receive updates.

Most software available on PyPI is free and open source. PyPI is supported by the Python Software Foundation and is maintained by an independent group of developers.

Module Description

arrow

Convert and format dates, times, and timestamps.

BeautifulSoup

Extract data from HTML and XML documents.

bokeh

Interactive plots and applications in the browser.

matplotlib

Static, animated, and interactive visualizations.

moviepy

Video editing, compositing, and processing.

nltk

Natural language toolkit for human languages.

numpy

Fundamental package for numerical computing.

pandas

Data analysis, time series, and statistics library.

pillow

Image processing for jpg, png, and other formats.

pytest

Full-featured testing tool and unit test framework.

requests

Elegant HTTP library for connecting to web servers.

scikit-learn

Simple, efficient tools for predictive data analysis.

scipy

Fundamental algorithms for scientific computing.

scrapy

Crawl websites and scrape data from web pages.

tensorflow

End-to-end machine learning platform for everyone.
Table 7.2 Example third-party modules available from PyPI.

Concepts in Practice

Third-party modules

Use pypi.org and the links in the table above to answer the questions.

4.
Which modules can be used to edit pictures and videos?
  1. BeautifulSoup and Scrapy
  2. Bokeh and Matplotlib
  3. MoviePy and Pillow
5.
Which third-party module is a replacement for the built-in datetime module?
  1. arrow
  2. calendar
  3. time
6.
Search for the webcolors module on PyPI. What function provided by webcolors looks up the color name for a hex code?
  1. hex_to_name
  2. name_to_hex
  3. normalize_hex

Exploring further

Programming blogs often highlight PyPI modules to demonstrate the usefulness of Python. The following examples provide more background information about the modules listed above.

Try It

Happy birthday

Module documentation pages often include examples to help programmers become familiar with the module. For this exercise, refer to the following examples from the datetime module documentation:

Write a program that creates a date object representing your birthday. Then get a date object representing today's date (the date the program is run). Calculate the difference between the two dates, and output the results in the following format:

    Your birth date: 2005-03-14
    Today's date is: 2023-06-01

    You were born 6653 days ago
    (that is 574819200 seconds)

    You are about 18 years old
    

Try It

More exact age

The datetime module does not provide a built-in way to display a person's exact age. Ex: The following program calculates an exact age (in years and days) using floor division and modulo. The output is: You are 15 years and 4 days old.

    from datetime import date

    birth = date(2005, 3, 14)
    today = date(2020, 3, 14) # 15 years later
    delta = today - birth
    
    years = delta.days // 365
    days = delta.days % 365
    print("You are", years, "years and", days, "days old")
    

Notice how leap years are included in the calculation. February 29th occurs four times between birth and today. Therefore, the user is not only 15 years old, but 15 years and 4 days old.

Many commonly used modules from PyPI, including arrow, are installed in the Python shell at python.org/shell. Open the Python shell and type the following lines:

    import arrow
    birth = arrow.get(2005, 3, 14)
    birth.humanize()
    

Refer to the humanize() examples from the arrow module documentation. In the Python shell, figure out how to display the number of years and days since birth using one line of code. Then display the number of years, months, and days since birth. Finally, use the print() function to output the results in this format: You are 18 years 4 months and 7 days old.

As time permits, experiment with other functions provided by the arrow module.

Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Mar 15, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.