Learn Python in 30 Days — Day 20: Creating Your Own Module

Day 20: Creating Your Own Module

Welcome to Day 20 of the Learn Python in 30 Days series!

Yesterday, you learned how to use Python’s built-in modules like math, random, and datetime.
Today, you’ll level up again by creating your own module  a huge milestone on your way from beginner to “I can build actual programs”.

This is where your Python projects become organised, reusable, and far easier to maintain.

All example files for this series are available on my GitHub: Learn-Python-in-30-Days

By the end of this lesson, you’ll know how to:

  • Split code into multiple files

  • Import your own functions

  • Build a tiny reusable module

  • Understand how Python finds modules

  • Use if __name__ == "__main__" properly

Step 1 — What Is a Module?

A module is simply a .py file you can import.

That’s it.

Built-in modules like math and random are written in the same way, you just don’t see the source.

Your own code can be imported the same way:

import mymodule

If you can write functions, you can write a module.

Step 2 — Create Your First Module File

Let’s build a simple utility module for a game something you might use in your Tiny Dungeon (Day 14) or future Python projects.

Create a file called tools.py

Inside it:

# tools.py """ A tiny module containing helper functions for games. """ def roll_dice(sides=6): """Return a random dice roll from 1 to `sides`.""" import random return random.randint(1, sides) def format_title(title): """Return a title wrapped with dashes for better display.""" return f"\n--- {title} ---\n"

You’ve just made a module!

Import Your Module in Another File

Now create a second file called main.py:

# main.py import tools print(tools.format_title("Welcome")) print("You rolled:", tools.roll_dice(20))

Run it:

python main.py

Try it yourself, hopefully you'll see something like that shown below: -

You can also download this example from my GitHub main.py and tools.py and run it yourself.

This is real, professional program structure, split code in files, import what you need.

How Python Finds Your Module

Python looks for modules in this order:

  1. The current folder (same directory as your script)

  2. Installed site-packages (pip installs)

  3. Standard library modules

As long as tools.py is in the same folder as main.py, the import will work.

Step 3 — Importing Specific Functions

Instead of importing the whole module, you can pick and choose:

from tools import roll_dice print(roll_dice())

Or rename them:

from tools import roll_dice as rd print(rd(12))

Clean, readable, flexible.

Step 4 — Using __name__ == "__main__"

This special block lets your module act as:

  • A library when imported

  • A standalone program when run directly

Add this to the bottom of tools.py:

if __name__ == "__main__": print("Testing tools module...") print(format_title("Demo")) print("Roll:", roll_dice(6))

Run it:

python tools.py

→ Runs the test code.

Try it yourself, hopefully you'll see something like that shown below: -

You can also download this example from my GitHub here and run it yourself.

Import it:

import tools

→ Does not run the test code.

This is a core Python pattern you’ll use for the rest of your programming life.

Mini-Project — Build a Text Helper Module

Create a module called textutils.py with:

  • A banner(text) function

  • A box(text) function

  • A warning(text) function

Example:

# textutils.py def banner(text): return f"\n===== {text} =====\n" def box(text): return f"[ {text} ]" def warning(text): return f"WARNING: {text}"

Then use it inside a separate file:

import textutils print(textutils.banner("Start Game")) print(textutils.box("Inventory Empty")) print(textutils.warning("Low Health"))

Try it yourself, hopefully you'll see something like that shown below: -

You can also download this example from my GitHub miniproject.py and textutils.py and run it yourself.

This small module can be reused in all your CLI tools going forward.

Wrap-Up — Why This Matters

Today you learned how to:

  • Make your own Python module

  • Import it into another file

  • Organise multi-file programs

  • Use __name__ == "__main__"

  • Build reusable functions that stay clean and separate

You’ve just stepped into the world of real software structure — the kind that powers actual applications, not just practice scripts.

Next Up — Day 21 – Week 3 Challenge

Tomorrow is Day 21 — Week 3 Challenge, where you’ll put modules + functions + files + error handling together into a Daily Journal CLI App.

All example files for this series are available on my GitHub: Learn-Python-in-30-Days

You can see the full series here Learn Python in 30 Days series!

Hope you have enjoyed this post, thanks Matty

Comments

Popular posts from this blog

Math Behind Logic Gates

6502 - Part 2 Reset and Clock Circuit

Building a 6502 NOP Test