Python Modules

Python · Functions & OOP

Theory

A module is simply a .py file containing functions, classes and variables that other code can import. A package is a folder of modules. import math loads the module and gives you the name math, so you write math.sqrt(16); from datetime import datetime brings one name in directly; import pandas as pd adds a short alias.

When you import, Python first checks whether the module is already loaded (sys.modules), then searches the folders listed in sys.path - your script's folder, the standard library and installed packages. The module's code runs once on first import and the result is cached. The standard library (math, json, datetime, re, os) ships with Python; third-party packages like pandas are installed with pip.

Why we use it

Modules let you split a large program into organised files and reuse code - your own or thousands of ready-made libraries - instead of writing everything from scratch.

When to use it

Import a module whenever you need its functionality, and move your own reusable functions into a module once a script grows. Avoid from module import *, which hides where names came from and can overwrite your own names.

How it works

  1. import math
  2. Already in sys.modules? Yes: Reuse the cached module. No: Search the folders in sys.path.
  3. Module file found? Yes: Run its code once and cache it. No: Raise ModuleNotFoundError.
  4. Bind the name math in your namespace
  5. Use math.sqrt(16) -> 4.0

Syntax

import module_name
from module import name

Example

import pandas as pd
from datetime import datetime

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic