Skip to main content
  1. Notes/
  2. Python ๐Ÿ/

๐Ÿ“ฆ Modules and Packages in Python | Lesson 12

Python Progmramming Course
Table of Contents
Python - This article is part of a series.
Part 12: This Article

๐Ÿ“ฆ Modules and Packages in Python
#

As you build bigger programs, you want to organize your code better and reuse useful pieces easily. Python makes this simple with modules and packagesโ€”building blocks that help you keep your code clean and manageable! Letโ€™s learn how to use them. ๐Ÿงฉ


What are Modules? ๐Ÿงฑ
#

A module is a file containing Python code (functions, classes, variables) that you can reuse by importing it into other programs.

How to Import a Module
#

Python comes with many built-in modules you can use right away:

import math

print(math.sqrt(16))  # 4.0

You can also import specific functions or variables:

from math import pi, ceil

print(pi)      # 3.141592653589793
print(ceil(4.2))  # 5

Creating Your Own Module ๐Ÿ› ๏ธ
#

Create a file called my_module.py with the following content:

def greet(name):
    return f"Hello, {name}!"

Use this module in another script:

import my_module

print(my_module.greet("Rahul"))  # Hello, Rahul!

What are Packages? ๐Ÿ“ฆ
#

A package is a folder/directory containing multiple Python modules, organized using a special __init__.py file.

It helps group related modules together.

my_package/
|-- __init__.py
|-- module1.py
|-- module2.py

You import like this:

from my_package import module1

module1.some_function()

Installing and Using External Packages ๐ŸŒ
#

Python has tons of packages you can install via pip (Pythonโ€™s package manager):

pip install requests

Then import and use:

import requests

response = requests.get("https://example.com")
print(response.status_code)

Practice Questions with Solutions ๐Ÿ†
#

  1. Use the built-in random module to print a random number between 1 and 10.
    • Solution:
import random
print(random.randint(1, 10))
  1. Create a module named math_utils.py with a function that returns the factorial of a number. Import and test it.
  2. Create a package with two modules: one with math functions and another with string functions. Experiment with importing them.

Mini Project: Simple Math Module ๐Ÿงฎ
#

Create a module simple_math.py:

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

Use in a separate file:

import simple_math

print(simple_math.add(10, 5))       # 15
print(simple_math.subtract(10, 5))  # 5

Checklist for This Chapter โœ…
#

  • Used built-in modules via import
  • Created and imported your own modules
  • Understood what packages are and how to import
  • Installed and used external packages with pip

Youโ€™re now ready to organize and reuse Python code efficiently! Explore Pythonโ€™s vast ecosystem and even build your own libraries! ๐Ÿš€

Aryan
Author
Aryan
A little bit about you
Python - This article is part of a series.
Part 12: This Article

Related

โฐ Working with Date and Time in Python | Lesson 16
Python Progmramming Course
๐ŸŽฌ Strings in Python | Lesson 04
Python Progmramming Course
๐Ÿ—๏ธ Object-Oriented Programming (OOP) Concepts in Python | Lesson 15
Python Progmramming Course
๐Ÿ‘ถ Python Basics | Lesson 02
Python Progmramming Course
๐Ÿ“„ File Handling in Python | Lesson 14
Python Progmramming Course
๐Ÿ“‹ Lists in Python | Lesson 05
Python Progmramming Course