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:
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:
'math': Mathematical functions'datetime': Date and time handling'random': Generate random numbers'os': Operating system functions'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:
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:
# my_module.py
def hello_world():
print("Hello, World!")
# main.py
import my_module
my_module.hello_world()
Hands-on Exercises
- Write a Python script that uses the
'math'module to calculate the area of a circle with a given radius. - Create a Python script that uses the
'random'module to generate and print 10 random numbers between 1 and 100. - 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
- Python documentation: https://docs.python.org/3/library/
- Real Python: https://realpython.com/python-modules-packages/
- W3Schools Python tutorial: https://www.w3schools.com/python/python_modules.asp
Summary and Next Steps
In this lesson, you have learned about:
- Modules and libraries in Python
- Importing modules and using their functions
- Python Standard Library
- Installing and using third-party libraries
- 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
- What is the purpose of the
'import'statement in Python? - Explain the difference between a module and a library in Python.
- How do you install a third-party library using
'pip'? - Write a Python script that uses the
'os'module to list all the files in the current directory. - 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.



