Skip to main content

Why organize your code?

As your scripts grow, they become harder to read and maintain. The solution? Split your code into functions and separate files. Benefits:
  • Reusable - Write once, use many times
  • Readable - Main script stays simple and clear
  • Testable - Easy to test individual functions

Creating helper functions

Let’s create simple helper functions for our sales analysis. In your sales-analysis folder, create a new file called helpers.py:
The :.2f in the format string rounds floating point numbers to 2 decimal places - perfect for displaying money!
That’s it! Two simple functions that do one thing each.

Using your functions

Now update analyzer.py to use these helpers:

How imports work

When you write from helpers import calculate_total:
  1. Python looks for helpers.py in the same folder
  2. It runs that file and makes the functions available
  3. You can now use calculate_total() directly
The file must be in the same folder for this simple import to work. We covered more complex imports in the Python paths section.

What you’ve accomplished

Look at what you’ve built! You started with a single Python file and now have:
  • An organized project structure
  • Understanding of how Python finds files
  • Code that reads real data and saves results
  • Reusable functions in separate files
This is how real Python projects work. You’re ready to build bigger things!

What’s next?

Now that you know how to structure and organize local Python projects, let’s put it all together to build an end-to-end weather data analysis project using APIs and visualization libraries!

Weather data analysis project

Build a weather analysis project using APIs and data files