LEARN COMPLETE PYTHON IN 24 HOURS

13. Modules & How to Use Them

A module is a file containing Python code (usually functions, classes, or variables) that you can import and use in other Python files.

13.1 What is a Module?

  • A module is just a .py file with Python code.

  • Python comes with many built-in modules (no installation needed).

  • You can also install external modules using pip (later topic).

  • You can create your own modules easily.

Why use modules?

  • Organize large programs (split code into files)

  • Reuse code in different projects

  • Use powerful libraries written by others (math, random, requests, pandas, etc.)

  • Keep code clean and maintainable

13.2 import, from … import, as keyword

There are different ways to bring code from a module into your program.

  1. Basic import

Python

import math print(math.sqrt(16)) # 4.0 print(math.pi) # 3.141592653589793

  1. import with alias (as) – very common

Python

import random as rnd print(rnd.randint(1, 100)) # random number between 1 and 100

  1. from … import … (import specific things)

Python

from math import sqrt, pi print(sqrt(25)) # 5.0 print(pi) # 3.141592653589793

  1. from … import * (import everything – avoid this in big projects)

Python

from random import * print(randint(1, 10)) # works directly

Best Practices (2026 standard):

  • Use import module or import module as short_name (most readable)

  • Use from module import specific_function when you need only 1–2 things

  • Avoid from module import * — can cause name conflicts

13.3 Popular Built-in Modules (math, random, datetime)

Python has many useful built-in modules — no installation needed!

1. math – Mathematical functions

Python

import math print(math.ceil(4.2)) # 5 print(math.floor(4.9)) # 4 print(math.pow(2, 3)) # 8.0 print(math.factorial(5)) # 120 print(math.sin(math.radians(30))) # 0.5 (sin 30°)

2. random – Generate random numbers/values

Python

import random print(random.random()) # random float 0.0 to 1.0 print(random.randint(1, 100)) # random integer 1–100 print(random.choice(["apple", "banana", "mango"])) # random fruit print(random.shuffle([1,2,3,4,5])) # shuffles list in place

3. datetime – Work with dates and times

Python

from datetime import datetime, date, timedelta # Current date & time now = datetime.now() print(now) # e.g. 2026-03-05 13:45:22.123456 # Format date print(now.strftime("%d %B %Y")) # 05 March 2026 print(now.strftime("%H:%M %p")) # 01:45 PM # Create specific date birthday = date(2000, 12, 15) print(birthday) # 2000-12-15 # Add/subtract days future = now + timedelta(days=30) print(future.date()) # 30 days from today

Quick Tip: These three modules are used in almost every real project!

13.4 Create Your Own Module

Very easy — just make a .py file and import it.

Step 1: Create a file named mymodule.py (in the same folder)

Python

# mymodule.py def greet(name): return f"Hello, {name}! Welcome to Python." def square(num): return num * num PI = 3.14159 print("This runs when module is imported") # runs only once

Step 2: Use it in another file (e.g., main.py)

Python

# main.py import mymodule print(mymodule.greet("Anshuman")) # Hello, Anshuman! Welcome to Python. print(mymodule.square(7)) # 49 print(mymodule.PI) # 3.14159

Alternative ways to import your module

Python

from mymodule import greet, square print(greet("Rahul")) # Hello, Rahul! Welcome to Python. print(square(10)) # 100 # or with alias import mymodule as mm print(mm.greet("Priya"))

Important Notes:

  • Module name = filename without .py

  • Code at the top level of module runs only once (when first imported)

  • To prevent code from running when imported, use this common pattern:

Python

# mymodule.py def greet(name): return f"Hello, {name}!" if name == "__main__": # This block runs only when you run the file directly # Not when imported print("This is running directly") print(greet("Test"))

Mini Project – Your Own Utility Module Create utils.py:

Python

# utils.py def celsius_to_fahrenheit(c): return (c 9/5) + 32 def is_prime(n): if n <= 1: return False for i in range(2, int(n*0.5) + 1): if n % i == 0: return False return True

Then use it:

Python

# main.py import utils print(utils.celsius_to_fahrenheit(30)) # 86.0 print(utils.is_prime(17)) # True print(utils.is_prime(15)) # False

This completes the full Modules section — now you can organize and reuse code like a professional!

Mini Projects & Practice Ideas. These small projects combine everything you've learned so far (variables, input/output, conditionals, loops, lists, dictionaries, functions, modules, etc.) — perfect for practice and building confidence!

All projects include full code with comments, explanations, sample output, and ideas to improve them.