Python Basics — Modules and Packages
Even the code size of tiny real-world applications is gigantic. In fact, it is common that an application contains thousands of lines of code. If you put the entire code in a single file, it becomes a really big file that you will not be able to debug the code later, and finding a specific code snippet is like looking for a needle in a haystack. Also, in most cases, we create our applications with preexisting codes, don’t forget that it is crucial to avoid reinventing the wheel.
In this lesson, we will learn more about Python modules and packages. Let’s dive in.
What is a Module?
The Python solution for managing gigantic codes is to group related functions, data, and classes (we will discuss classes later) and place them in a separate file with an.py
extension called a module. So, technically, every Python source code file is a module. You can create your modules, use the other programmers’ modules, and import them into your program.
What is a Package?
A package is a group of related modules. Packages are used to organize a large program into smaller subsets that are easier to maintain and can be imported separately. In Python terminology, commonly used packages are called libraries. The Python programming language provides the Python Standard Library that’s used for a wide variety of everyday programming tasks.
Importing is the process of obtaining code found in external files. So, we import a package or library to use the code it contains by simply adding an import
statement with the name of the library or package, and Python can find it. However, if Python cannot find it, you need to install the package on your own system by using pip
or conda
package installers.
There are thousands of packages available on the internet, but among them, there are some popular packages that are used in data science, such as NumPy for working with multi-dimensional arrays efficiently, Pandas for data analysis and manipulation, Matplotlib for data visualization, or Scikit-learn for building machine learning models.
For example, the following snippet returns the square root of 81
by calling the sqrt
function in the math
package.
import
math
print(math.sqrt(81))
Go ahead and run it. On the first line, we imported the math module. (you may see modules used in place of packages; the two terms are usually used interchangeably.) Then by calling the sqrt function that belongs to the math module, printed the square root of 81 out.
Importing a package makes all functionality from the package available in our code. However, it is possible to use a specific part of a package in the code. Let’s see how to make our import
statement more selective:
from
math import
pi
radius =
float(input("Enter the radius of the circle: "))
area =
pi*radius**2
print("Circle Area = {:.2f}".format(area))
Go ahead and run the cell. First, we just imported the pi
constant (π =3.1415) from the math
package, and then used it for calculating the area of a circle by getting its radius from the user. Then, I used the formatted print statement to output the circle’s area in two decimal places, {:.2f}
.
We will learn more about packages in the following lesson. Alright, I hope you enjoyed this lesson. In the next lesson, we will talk about Python dictionaries.