Sitemap
D Naresh

D Naresh

Modules & Packages in Python

Python Made Easy - (Article 14) Telugu Explanation

3 min readNov 1, 2025

--

Press enter or click to view image in full size

1. What is a Module?

Module ante - oka Python file (.py) lo manam functions, variables, or classes create chesi,
vere file lo reuse cheyyadam.

Simple ga cheppali ante:
“Manam code ni divide chesukoni, separate files lo petti, appudu neat ga organize chesukovachu.”

2. Why use Modules?

Press enter or click to view image in full size
  • Code ni re-use cheyyachu (copy-paste avasaram ledu)
  • Code clean & organized ga untundi
  • Large projects lo teamwork easy avuthundi

3. Creating and Using Your Own Module

Let’s create a real-life example module:

Press enter or click to view image in full size

Step 1: Create a file shopping.py

def buy_item(item, price):
print(f"You bought {item} for ₹{price}")
def offer(price):
discount = price * 0.10
return price - discount
def thank_you():
print("Thank you for shopping with us 🛍️")

Step 2: Create another file main.py

import shopping
shopping.buy_item("Shoes", 1500)
discounted_price = shopping.offer(1500)
print(f"Discounted Price: ₹{discounted_price}")
shopping.thank_you()

Output:

You bought Shoes for ₹1500
Discounted Price: ₹1350.0
Thank you for shopping with us 🛍️

Explanation:

  • import shopping → brings in the shopping.py file
  • shopping.buy_item() → calls that function from the module
  • Manaki ekkada ayina shop related logic kavali ante, idi reuse cheyyachu

3. Import Variations in Python

(1) Import Entire Module

import math
print(math.sqrt(25))

(2) Import Specific Function

from math import sqrt
print(sqrt(36))

(3) Import Multiple Items

from math import sqrt, pi
print(sqrt(49))
print(pi)

(4) Rename (Alias)

import math as m
print(m.factorial(5))

4. Built-in Modules Examples

Python lo already hundreds of built-in modules unnayi.
Let’s look at few most useful ones

Get D Naresh’s stories in your inbox

Join Medium for free to get updates from this writer.

Example 1: datetime Module

import datetime
today = datetime.date.today()
print("Today's Date:", today)
time_now = datetime.datetime.now()
print("Current Time:", time_now)

Output:

Today's Date: 2025-10-11
Current Time: 2025-10-11 17:42:30.123456

Example 2: random Module

import random
num = random.randint(1, 10)
print("Lucky number:", num)
fruits = ["Apple", "Banana", "Mango"]
print("Random fruit:", random.choice(fruits))

Output:

Lucky number: 7
Random fruit: Mango

Example 3: os Module (Working with files & folders)

import os
print("Current Directory:", os.getcwd())
print("Files:", os.listdir())

5. What is a Package?

Press enter or click to view image in full size

Package = a folder containing multiple modules + a special file __init__.py.

Example structure:

my_package/
│
├── __init__.py
├── billing.py
├── customer.py
└── discount.py

billing.py

def total_amount(amount, tax):
return amount + (amount * tax / 100)

customer.py

def customer_info(name, city):
print(f"Customer: {name}, City: {city}")

discount.py

def apply_discount(price):
return price - (price * 0.15)

main.py

from my_package import billing, customer, discount
customer.customer_info("Naresh", "Hyderabad")
bill = billing.total_amount(1000, 5)
print("Total Amount with Tax:", bill)
final_price = discount.apply_discount(bill)
print("After Discount:", final_price)

Output:

Customer: Naresh, City: Hyderabad
Total Amount with Tax: 1050.0
After Discount: 892.5

6. Installing & Using External Packages

Sometimes, you need extra power
For that, use external packages from the internet (PyPI).

Example: install emoji package

pip install emoji

Usage:

import emoji
print(emoji.emojize("I love Python :snake: ❤️"))

Output:

I love Python 🐍 ❤️

7. Key Takeaways

Press enter or click to view image in full size

8. Practice Exercises

1️.Create a module calculator.py with functions for add, subtract, multiply, divide.
2️.Create a module greetings.py with functions like say_hello(), say_bye().
3️. Create a package myshop with files: items.py, offers.py, billing.py.

  • items.py → list of items
  • offers.py → discount functions
  • billing.py → calculate total
  • Use all in main.py

4️. Install one external module (like requests, emoji, or pyjokes) and try using it.

Summary:

Module → Single Python file
Package → Folder containing modules
Built-in Modules → Already available
External Modules → Installed using pip
import, from ... import, as → Used for importing

Previous & Next

Next Article : 👉15.Writing Test Cases using unittest module
Previous Article: 👉13.Exception Handling in python

--