TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun ยท 77.8K subscribers
Post #4476 2.46K
๐Ÿš€ Data Science Roadmap 2026

๐Ÿ“˜ Phase 1: Programming Fundamentals

๐Ÿ Topic 10: Python Modules, Packages & File Handling

Welcome back! ๐Ÿ‘‹

So far, you've learned Python fundamentals, functions, data structures, list comprehensions, and functional programming. In this lesson, you'll learn how to organize your code into modules and packages and how to read from and write to files.

These skills are essential for every Data Scientist because real-world projects involve working with multiple Python files, libraries, and datasets stored in files.

๐Ÿ”น 1. What is a Module?

A module is a Python file (".py") that contains functions, variables, or classes that can be reused in other Python programs.

Instead of writing the same code repeatedly, you can create a module once and import it wherever needed.

Example:

Suppose you have a file named calculator.py

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

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


Now use it in another file:

import calculator

print(calculator.add(10, 5))


Output: 15

๐Ÿ”น 2. Importing Modules

Python provides different ways to import modules.

Import the Entire Module

import math
print(math.sqrt(25))


Output: 5.0

Import Specific Functions

from math import sqrt
print(sqrt(49))


Output: 7.0

Import with an Alias

Aliases make long module names easier to use.

import math as m
print(m.pi)


Output: 3.141592653589793

๐Ÿ”น 3. Common Built-in Modules

Some commonly used Python modules are:

โ€ข "math" โ†’ Mathematical operations

โ€ข "random" โ†’ Generate random numbers

โ€ข "datetime" โ†’ Work with dates and times

โ€ข "os" โ†’ Interact with the operating system

โ€ข "sys" โ†’ Access system-specific information

โ€ข "statistics" โ†’ Perform statistical calculations

Example:

import random
print(random.randint(1, 10))


This generates a random integer between 1 and 10.

๐Ÿ”น 4. What is a Package?

A package is a collection of related modules organized into folders.

Example:

project/
โ”‚
โ”œโ”€โ”€ main.py
โ”œโ”€โ”€ utilities/
โ”‚ โ”œโ”€โ”€ init.py
โ”‚ โ”œโ”€โ”€ calculator.py
โ”‚ โ””โ”€โ”€ helper.py


Packages help organize large Python projects into manageable sections.

๐Ÿ”น 5. File Handling

Most Data Science projects involve reading data from files such as:

โ€ข CSV files

โ€ข Text files

โ€ข Excel files

โ€ข JSON files

Python provides built-in functions for file handling.

๐Ÿ”น 6. Opening a File

Syntax: open(file_name, mode)

Common modes:

Mode | Description

"r" | Read

"w" | Write (overwrites existing content)

"a" | Append

"x" | Create a new file

"rb" | Read binary files

"wb" | Write binary files

๐Ÿ”น 7. Reading a File

Suppose sample.txt contains:

Welcome to Python
Learning File Handling


Python code:

file = open("sample.txt", "r")
print(file.read())
file.close()


Output:

Welcome to Python
Learning File Handling


๐Ÿ”น 8. Writing to a File

file = open("sample.txt", "w")
file.write("Hello Data Science!")
file.close()


This replaces the previous contents of the file.

๐Ÿ”น 9. Appending to a File

file = open("sample.txt", "a")
file.write("\nPython is awesome!")
file.close()
  • โค 3
More from @datasciencefun
  1. Oct 7, 2026ORDER BY salary DESC LIMIT 5; Mistake 3 โ€” Forgetting that LIMIT applies after sorting Forโ€ฆ
  2. Oct 7, 2026๐Ÿš€ Data Science Roadmap 2026 ๐Ÿ“ Phase 3: SQL for Data Science ๐Ÿ“– Topic 4 โ€” LIMIT LIMIT isโ€ฆ
  3. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  4. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  5. Oct 2, 2026Data Visualisation tips for beginners
  6. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’