Organise Code: Functions & Modules
The first step from beginner to capable is organisation โ breaking a program into well-named functions and splitting it across modules so it stays readable as it grows.
You can write Python that works. The next skill is writing Python that stays workable as it grows - and that starts with organisation. A good program isn't one long script; it's a set of small, well-named functions, grouped into modules you can reuse. This session is about structuring code so it stays clear, testable and easy to change.
Functions: name a piece of work
A function gives a name to a chunk of work, so you can write it once, test it once, and reuse it everywhere. The art is making each one do one clear thing.
# vague: one long blob
# better: small, named pieces that read like a sentence
def clean(name):
return name.strip().title()
def greet(name):
return "Hello, " + clean(name) + "!"
greet(" ada ") # 'Hello, Ada!'Modules: group related functions
As a program grows, you split functions across modules - separate .py files - and import what you need. This keeps each file focused and lets you reuse code across projects.
# in text.py
def clean(name):
return name.strip().title()
# in main.py
from text import clean
print(clean(" ada "))| Idea | What it does |
|---|---|
| def name(args): | Define a reusable piece of work |
| return value | Hand a result back to the caller |
| import module | Use code from another file |
| from m import thing | Bring one name in directly |
Python's standard library is thousands of ready-made modules (math, random, json, datetime...). Learning to find and import the right one is a core skill - you rarely need to build from scratch.
Structure as you grow
- Functions name a piece of work - keep each to one clear job.
- Good names make code read like a sentence and reduce comments.
- Modules group related functions into files you import and reuse.
- The standard library is a huge toolbox - import, don't reinvent.
๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- A good program is small well-named functions grouped into modules, not one long script.
- A function should do one thing; if you can't name it clearly, split it.
- Modules (separate .py files) group related functions you import and reuse.
- The standard library is a huge toolbox - learn to import the right module rather than reinventing.
Frequently asked
When should I break code into a function?
A good rule: whenever you catch yourself copying and pasting code, or writing a comment to explain what the next few lines do, that's a signal to make a function. Repetition is the clearest trigger - if the same logic appears twice, put it in a function and call it twice, so a fix or change happens in one place. A comment like '# now validate the email' often means those lines want to become a validate_email() function whose name replaces the comment. Beyond that, break out a piece of work when it has a clear single purpose you could name, when it's getting long enough that you lose the thread, or when you'd like to test it on its own. Don't over-fragment tiny scripts, but lean toward more, smaller functions as programs grow - it almost always reads better.
What's the difference between 'import module' and 'from module import thing'?
Both bring in code from another file; they differ in how you then refer to it. 'import math' brings in the whole module, and you use its contents with the module name as a prefix: math.sqrt(16). 'from math import sqrt' brings in just that one name, and you use it directly: sqrt(16). Use plain 'import module' when you want several things from a module or want it clear where a function comes from (math.sqrt reads unambiguously); use 'from module import thing' when you use one or two names a lot and the prefix would be noise. Avoid 'from module import *' (importing everything unqualified) - it makes it unclear where names came from and can cause clashes. Both are just tools for reusing code across files; pick whichever keeps the calling code clearest.
Related lessons
Working with Files
Real programs read and write files โ save data, load it back, process a document. It's simโฆ
Intermediate PythonErrors & Exceptions
Things go wrong โ a file is missing, input is nonsense, the network is down. Handling erroโฆ
Python Fundamentals (Hands-On)Functions: Reusable Building Blocks
Functions let you name a chunk of code and reuse it. They're how you avoid repetition, orgโฆ