Mini project — Organising a program into modules
Lesson 22 of 26 · Basic Python
In this mini project you will learn how to organise your Python program into several files (modules). Instead of putting all the functions in one file, we will make modules that each have their own purpose.
Project description
Let us make a small program that uses:
- maths functions (addition, multiplication)
- functions for working with text (reverse a string, count words)
- helper functions (greet the user, print a message)
The goal is for each module to contain logically related functions, while the main program combines all the modules.
File structure
modular_project/ │ ├── main.py ├── maths.py ├── text_tools.py └── helper_functions.py
Example module contents
# maths.py
def add(a, b):
return a + b
def multiply(a, b):
return a * b
# text_tools.py
def reverse_string(s):
return s[::-1]
def count_words(s):
return len(s.split())
# helper_functions.py
def greet():
print("Hello! Welcome to the modular project.")
The main program
# main.py
import maths
import text_tools as tt
from helper_functions import greet
greet()
print("Addition 5 + 7 =", maths.add(5, 7))
print("Multiplication 4 * 6 =", maths.multiply(4, 6))
text = "Python modular programming"
print("Reversed string:", tt.reverse_string(text))
print("Number of words:", tt.count_words(text))
Tasks for extending the project
- Add a new module
validation.pythat checks whether the input is a number. - Add a function that calculates the factorial to
maths.pyand call it from the main program. - Combine all the modules in one function in
main.pythat runs the whole program interactively (e.g. asks the user for input). - Try the
import asalias and see how the way you call functions changes.
This way you learn how larger programs are really organised and gain experience with modular thinking.
