Modules and Packages

Modules and packages are useful for organization of Python code making our code clean, modular and easy to maintain.

Python Modules

In Python, a module is a file containing Python code, which can include functions, classes, or variables. Modules help us to organize and reuse our code effectively.

Note: For the following examples, you need to create manually mod.py and the new file using your own code editor, simply copy and paste the code into two separate files.

Create a Module

To create a module we simply write our Python code into a file and give it a name with .py extension, in this case we shall name it mod.py.

#mod.py
s = "I'm learning Python in order to become an AI engineer."
a = [100, 200, 300]

def foo(arg):
    print(f'arg = {arg}')

Using Modules

To use a module, we use the keyword import. After importing a module, you can use its functions and classes by using the module name followed by a dot, and then the function or class you want to use.

import mod
print(mod.s)

mod.a

mod.foo(['quux', 'corge', 'grault'])

Package Management with pip

pip is the package manager for Python, and it allows you to easily install, update, and remove packages. Packages are collections of modules that provide additional functionality. You can download packages from the Python Package Index (PyPI).

Basic commands

Here is a list of basic commands for package management with pip:

  • To install a package: pip install package_name
  • To update a package: pip install --upgrade package_name
  • To remove a package: pip uninstall package_name
  • To list installed packages: pip list

Requirements file

A requirements.txt file helps you to manage package dependencies for your project. It lists the package names with their version numbers that your project depends on.

You can create this file manually or generate it using pip freeze > requirements.txt.

The file should list one package per line

numpy==1.21.2
pandas==1.3.3
matplotlib==3.4.3

Use the pip install -r requirements.txt command to update the installed packages according to the new requirements.txt file.

Using Modules from Packages

First, we need to install the packages using pip:

pip install pandas numpy

Then, you can import a module in your Python script using the import statement.

import pandas as pd
import numpy as np

# Create a simple dataset
data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'David'],
    'Age': [25, 30, 35, 40],
    'Salary': [50000, 55000, 60000, 65000]
}

# Convert the dataset to a pandas DataFrame
df = pd.DataFrame(data)

# Calculate the mean of the Age column using numpy
mean_age = np.mean(df['Age'])
print(f'Mean Age: {mean_age}')

# Calculate the sum of the Salary column using pandas
total_salary = df['Salary'].sum()
print(f'Total Salary: {total_salary}')

# Add a new column named 'Salary_with_bonus' to the DataFrame
df['Salary_with_bonus'] = df['Salary'] * 1.1
print(df)

Exercise Modules and Packages

!pip install rggrader
# @title #### Student Identity
student_id = "your student id" # @param {type:"string"}
name = "your name" # @param {type:"string"}
# @title #### 00. Install and Use a Package
from rggrader import submit

# TODO: 
# 1. Import the numpy package with alias "np". Note: you do not need to install numpy using pip in this context, assume it is already installed.
# 2. Use numpy's function that can calculate the average of the list [1, 2, 3, 4, 5] and assign the output to a variable named 'average'.


# Put your code here:
average = 0


# ---- End of your code ----

# Do not modify the code below. It is used to submit your solution.
assignment_id = "05-modules-and-packages"
question_id = "00_install_use_package"
submit(student_id, name, assignment_id, str(average), question_id)

# Example:
# Package: numpy with alias "np"
# Function: Calculate the sum of the list [10, 20, 30, 40, 50]
# Output: 150
# @title #### 01. Standard Deviation of list elements
from rggrader import submit

# TODO: 
# 1. Import numpy package with alias "np".
# 2. Use numpy's function that can calculate the standard deviation of elements in the list [1, 2, 3, 4, 5] and assign the output to a variable named 'std_dev'. 


# Put your code here:
std_dev = 0


# ---- End of your code ----

# Do not modify the code below. It is used to submit your solution.
assignment_id = "05-modules-and-packages"
question_id = "02_std_dev_of_list"
submit(student_id, name, assignment_id, str(std_dev), question_id)

# Example:
# Package: numpy with alias "np"
# Function: Calculate the standard deviation of the list [10, 20, 30, 40, 50]
# Output: 14.142136
# @title #### 02. Create a DataFrame using pandas
from rggrader import submit

# TODO: 
# 1. Import pandas package with alias "pd".
# 2. Use pandas DataFrame constructor to create a DataFrame from the dictionary: {"Name": ["Anna", "Bob", "Charlie"], "Age": [21, 25, 30]}.
#    Assign the output to a variable named 'df'.


# Put your code here:
df = None


# ---- End of your code ----

# Do not modify the code below. It is used to submit your solution.
assignment_id = "05-modules-and-packages"
question_id = "03_create_dataframe"
submit(student_id, name, assignment_id, df.to_string(), question_id)

# Example:
# Package: pandas with alias "pd"
# Function: Create a DataFrame from the dictionary: {"Country": ["Finland", "Sweden", "Norway"], "Population": [5.5, 10.4, 5.4]}
# Output: 
#    Country  |  Population
#    Finland  |  5.5
#    Sweden   |  10.4
#    Norway   |  5.4
Back to top