Skip to content
All tutors

Functions, Modules and Tidy Scripts

Turn one long copy and paste script into small functions and modules you can reuse and trust.

By Skill Lab Beginner Python for AI New

Your first 3 replies from the tutor are free. Then you can continue the lesson for $3 from your credit.

A taste of a lesson

One question and the answer, as this tutor gives it.

Functions, Modules and Tidy Scripts AI tutor following Skill Lab's plan
Student:

My script is 300 lines with the same cleaning code copied four times. Where do I start?

Tutor:

Start with exactly that copied cleaning code, because it is the clearest win. Compare the four copies and note what differs between them: maybe a column name or one extra step. Those differences become parameters. Write one function, say clean_column(rows, column), that returns the cleaned rows, replace a single copy with a call to it, and run the script to check the output is unchanged. Then replace the other three. What differs between your four copies?

Written with AI under Skill Lab's direction as an example. In your lesson the tutor answers your own questions, and like any AI it can be wrong. Right now the AI model is not connected: replies come from a built in test engine on this site.

What you will be able to do

  • Write small functions with clear parameters and return values
  • Split a long script into modules with the main guard
  • Read settings from the command line and environment instead of editing code
  • Replace scattered print calls with simple logging
  • Avoid shadowed imports, circular imports and hidden global state

Lesson plan

6 lessons. Pick one to start there.

  1. 1 Spotting functions inside a long script Identify repeated or nameable blocks that should become functions. Start
  2. 2 Inputs, outputs and return values Write functions that take parameters and return results instead of printing. Start
  3. 3 Modules, imports and the main guard Split code into modules and make files safe to import. Start
  4. 4 Settings without editing code Move paths, options and secrets out of the code body. Start
  5. 5 Logging instead of print Use the logging module to see what a script did and when. Start
  6. 6 Refactor a real script Restructure a working script into functions and modules without changing what it does. Start

Try asking

Tap a question to start a lesson with it.

About this tutor

For beginners whose AI and data scripts have grown into one long file full of repeated blocks and global variables. You learn to write small functions with clear inputs and return values, split code into modules, use the main guard, read settings from the command line or environment, and replace scattered print calls with simple logging. The tutor works from a script you bring, or a provided one that cleans data and calls an API, and refactors it with you step by step, running it after every change so nothing breaks. You leave with a structure you can repeat for future projects.

Reviews

Students can review a tutor after a paid lesson. Nobody has yet.