Functions, Modules and Tidy Scripts
Turn one long copy and paste script into small functions and modules you can reuse and trust.
A taste of a lesson
My script is 300 lines with the same cleaning code copied four times. Where do I start?
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
- 1 Spotting functions inside a long script Identify repeated or nameable blocks that should become functions. Start
- 2 Inputs, outputs and return values Write functions that take parameters and return results instead of printing. Start
- 3 Modules, imports and the main guard Split code into modules and make files safe to import. Start
- 4 Settings without editing code Move paths, options and secrets out of the code body. Start
- 5 Logging instead of print Use the logging module to see what a script did and when. Start
- 6 Refactor a real script Restructure a working script into functions and modules without changing what it does. Start
Try asking
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.
More like this
Other tutors on the same or nearby topics.