Opens in a new tab
Lesson 4

Modules and Libraries in Python

Modules & Libraries, Python Mastery Program, Stage 1 Python

Written by

Muhammad Atif

Last Updated

Introduction to Modules and Libraries

In Python, a module is a file containing Python code, such as functions, classes, and variables. Libraries, also known as packages, are collections of related modules. Modules and libraries can be imported into your Python scripts to extend their functionality.

Importing Modules

To import a module in Python, use the 'import' statement followed by the module name. You can also import specific functions or classes using the ‘from' keyword.

Example:

Python
import math
from datetime import date

The Python Standard Library

Python comes with a built-in standard library that contains a wide range of modules for various tasks. Some commonly used modules include:

  1. 'math': Mathematical functions
  2. 'datetime': Date and time handling
  3. 'random': Generate random numbers
  4. 'os': Operating system functions
  5. 're': Regular expressions

Installing and Using Third-Party Libraries

Python’s ecosystem has many third-party libraries available for different tasks. You can install these libraries using the package manager 'pip'. To install a library, run 'pip install library_name' in the terminal or command prompt.

Example:

Python
pip install requests

Creating and Organizing Your Own Modules

You can create your own modules by writing Python code in a .py file. To use the functions and classes defined in your module, import it into your script using the same syntax as for built-in modules.

Example:

Python
# my_module.py
def hello_world():
    print("Hello, World!")

# main.py
import my_module
my_module.hello_world()

Hands-on Exercises

  1. Write a Python script that uses the 'math' module to calculate the area of a circle with a given radius.
  2. Create a Python script that uses the 'random' module to generate and print 10 random numbers between 1 and 100.
  3. Use the 'datetime' module to calculate the number of days between two dates.

Real-World Example

Suppose you are building a weather app that needs to fetch data from a weather API. You can use the 'requests' library to make HTTP requests and fetch the data. Then, you can process and display the data using your Python script.

Additional Resources

  1. Python documentation: https://docs.python.org/3/library/
  2. Real Python: https://realpython.com/python-modules-packages/
  3. W3Schools Python tutorial: https://www.w3schools.com/python/python_modules.asp

Summary and Next Steps

In this lesson, you have learned about:

  1. Modules and libraries in Python
  2. Importing modules and using their functions
  3. Python Standard Library
  4. Installing and using third-party libraries
  5. Creating and organizing your own modules

In the next lesson, you will learn about file I/O and exception handling in Python. This knowledge will help you create more robust and versatile programs.

Remember to review the material periodically and practice your skills to reinforce your learning. Good luck on your journey to becoming a Python developer!

Review Questions

  1. What is the purpose of the 'import' statement in Python?
  2. Explain the difference between a module and a library in Python.
  3. How do you install a third-party library using 'pip'?
  4. Write a Python script that uses the 'os' module to list all the files in the current directory.
  5. Create a Python module with a function that returns the factorial of a given number, and then use the module in a separate script to calculate the factorial of a user-provided number.

Muhammad Atif

Data Analyst & Web Development Professional | Focused on Machine Learning, SQL and Python

Transitioned from a background in finance to IT, I now focus on data science and web development, with specialties in Machine Learning, SQL, and Python. My articles aim to clarify complex IT concepts, informed by a rich history in analytical thinking and strategy.

You Might Also Like

Browse more topics and insights handpicked just for you. Stay updated and informed with our latest articles and news.