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 by the teacher as an example. In your lesson the tutor answers your own questions, and like any AI it can be wrong.
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
4.5
2 ratingsSample
- Marta S.Sample
I had named my file json.py and could not understand the import error. Found that in the first lesson. The main guard explanation was very clear too.
- Yusuf T.Sample
We refactored my actual scraping script. Changing one thing and running it each time felt slow but nothing broke, which never happened when I tried alone.
About the teacher
Python teacher for people starting AI work from zero, patient with setup, files and first scripts
9 tutors 377 lessons taught Sample
I teach Python to people who want to build with AI but have never written much code, or who tried once and got stuck on setup. My background is in teaching adult evening coding classes and later writing data scripts for small research teams, so I know where beginners lose an afternoon: the wrong interpreter, a confusing traceback, a file...
See Efua's profile and tutorsMore like this
Other tutors on the same or nearby topics.