HomeCourseModule 05 › Anatomy of a real script

Anatomy of a real script

Module 05 · Functions8 min readBeginner

What you'll learn

  • Recognise the standard layout of a Python script
  • Use the if __name__ == '__main__' idiom
  • Write your own well-organised script

The standard layout

"""monthly_report.py — generate the monthly sales report.

Usage: python monthly_report.py
"""

# ---- 1. Imports (standard library first, then third-party, then your own) ----
import os
from datetime import date

import pandas as pd

from helpers import clean_email

# ---- 2. Constants ----
INPUT_FILE  = "sales_raw.csv"
OUTPUT_FILE = "sales_clean.xlsx"
TAX_RATE    = 0.08

# ---- 3. Functions ----
def load(path):
    return pd.read_csv(path)

def clean(df):
    df["email"] = df["email"].apply(clean_email)
    df["total"] = df["price"] * (1 + TAX_RATE)
    return df

def save(df, path):
    df.to_excel(path, index=False)

def main():
    df = load(INPUT_FILE)
    df = clean(df)
    save(df, OUTPUT_FILE)
    print(f"Wrote {len(df)} rows to {OUTPUT_FILE}")

# ---- 4. The "run me" line ----
if __name__ == "__main__":
    main()

What's that if __name__ == "__main__" thing?

It's the line that says: "Run main() only if I was launched directly, not when someone imports me from another file."

Why it matters: if you put loose code at the bottom of the file, that code runs even when you just import the file from somewhere else. The if __name__ idiom prevents that.

The benefit of breaking into functions

💡 The 30-second rule
If you can describe a piece of code in one short sentence, it's probably a function. "Clean the email column" → clean_emails(df). "Read the input file" → load(path).

Key takeaways

  • Imports → constants → functions → main()if __name__ == "__main__".
  • The if __name__ guard stops your main() from running on import.
  • Small functions beat a giant script every time.

Refactor your tip calculator

Take the tip-calculator script you wrote in Module 3 and split it into three functions: get_inputs(), compute(bill, tip_pct, people), and print_results(...). Call them from main().

📹 Video walkthrough
A video walkthrough of this lesson will be embedded here. Until then, the written walkthrough above mirrors what the video will cover step-for-step.