๐Ÿ
๐Ÿ Intermediate Python ยท PY-201 ยท Session 1 of 6

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.

โฑ 8 min read๐Ÿ“… Updated 2026-07-28๐Ÿ“Š Tables, charts & examples

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!'
๐ŸŽฏ
A function should do one thing, and its name should say what. If you can't name a function clearly, it's probably doing too much - split it. Small, well-named functions are the single biggest thing that makes code readable: greet() calling clean() reads almost like English, and each piece can be understood and tested on its own. Aim for functions short enough to see at a glance.

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  "))
IdeaWhat it does
def name(args):Define a reusable piece of work
return valueHand a result back to the caller
import moduleUse code from another file
from m import thingBring 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.

Session 1 of 6 complete

Next up: Working with Files

Continue to Session 2 โ†’

Related lessons

More in this course

Course home → · Browse all courses →