How 30 Days of Python Teaches Modules and Packages: A Hands-On Approach

The 30 Days of Python course teaches modules and packages through incremental, hands-on creation, starting with single .py files on Day 12 and advancing to directory-based packages on Day 20.

The Asabeneh/30-Days-Of-Python repository structures learning modules and packages across two dedicated days, emphasizing practical code organization over abstract theory. By Day 12, learners write reusable functions in standalone files, while Day 20 expands this mental model to multi-module directories with explicit import mechanics.

The Progressive Learning Structure

The curriculum separates these concepts intentionally to build complexity gradually.

Day 12: From Script to Module

On Day 12, the course establishes that a module is simply any Python file containing reusable code. The learning approach centers on immediate application: you create mymodule.py, populate it with functions like generate_full_name(), then import it into main.py to witness namespace separation firsthand.

According to the source code in 12_Day_Modules/mymodule.py, you define utility functions without execution logic:


# mymodule.py

def generate_full_name(firstname, lastname):
    """Return a full name string."""
    return f"{firstname} {lastname}"

The companion file 12_Day_Modules/main.py demonstrates three import patterns essential for learning modules and packages:


# Pattern 1: Module-level import

import mymodule
print(mymodule.generate_full_name('Asabeneh', 'Yetayeh'))

# Pattern 2: Selective import

from mymodule import generate_full_name

# Pattern 3: Aliased import

from mymodule import generate_full_name as fullname

Day 20: Organizing Code into Packages

Day 20 introduces packages as directories containing related modules, marked by the presence of __init__.py. The repository stores these examples in 20_Day_Python_package_manager/mypackage/, demonstrating how professional Python projects scale beyond single files.

The directory structure follows Python packaging conventions:


mypackage/
│   __init__.py
│   arithmetics.py
│   greet.py

In 20_Day_Python_package_manager/mypackage/arithmetics.py, mathematical utilities are grouped logically:

def add_numbers(*args):
    return sum(args)

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

def power(a, b):
    return a ** b

Meanwhile, 20_Day_Python_package_manager/mypackage/greet.py handles presentation logic:

def greet_person(firstname, lastname):
    return f'{firstname} {lastname}, welcome to 30DaysOfPython Challenge!'

Import Mechanics and Namespace Control

The pedagogical approach stresses that packages extend the import syntax without introducing new keywords. You access submodules using dot notation, as shown in the Day 20 documentation:

from mypackage import arithmetics, greet

# Package-level access

arithmetics.add_numbers(1, 2, 3, 5)  # Returns 11

# Module-level function access

greet.greet_person('Asabeneh', 'Yetayeh')

The __init__.py file (located at 20_Day_Python_package_manager/mypackage/__init__.py) serves as the package initializer, though the course notes it can remain empty for basic functionality. This demystifies the magic behind Python imports while preparing learners for advanced __init__.py configurations in larger codebases.

Reinforcing Concepts Through the Standard Library

The course reinforces learning modules and packages by having learners import built-in modules like os, sys, math, and random using identical syntax to user-created modules. This demonstrates that Python's standard library follows the same architectural patterns taught in the custom examples, bridging the gap between educational exercises and production code.

Summary

  • Day 12 focuses on single-file modules (mymodule.py), teaching that any .py file is importable.
  • Day 20 expands to directory-based packages with __init__.py, grouping related modules in mypackage/.
  • The curriculum uses generate_full_name, add_numbers, and greet_person as concrete examples of reusable code units.
  • Import variations (import, from, as) are practiced consistently across both days to solidify namespace mechanics.
  • The Asabeneh/30-Days-Of-Python repository structures this progression to mirror professional Python project organization.

Frequently Asked Questions

What is the difference between how Day 12 and Day 20 approach code organization?

Day 12 treats code organization at the file level, where you write functions in a single .py file and import it as a module. Day 20 advances this to the directory level, where you organize multiple related modules into a folder containing __init__.py, creating a package that can be imported as a unified unit.

Why does the 30 Days of Python course use __init__.py in the mypackage folder?

The __init__.py file in 20_Day_Python_package_manager/mypackage/__init__.py marks the directory as a Python package, enabling the import system to recognize it as a collection of modules rather than a generic folder. While it can be empty for basic functionality, its presence is mandatory for the from mypackage import arithmetics syntax to function correctly in older Python versions and remains a convention in modern Python 3.

How does the course demonstrate real-world module usage?

The course bridges theory and practice by having you write actual utility functions like generate_full_name() and add_numbers(), then import them into separate execution scripts. This mirrors professional workflows where utils.py or helpers.py modules are imported by main application files, reinforcing the code → module → package → import mental model used in production Python development.

Can I apply the Day 12 and Day 20 patterns to built-in Python modules?

Yes, the import syntax remains identical. The course explicitly has you practice with os, sys, math, and random to prove that built-in modules follow the same rules as your custom mymodule.py. This consistency is central to the course's approach to learning modules and packages, showing that Python's entire ecosystem relies on these fundamental import mechanics.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →