Lesson 1 of 9 · 40 min · UG / diploma
Applications, not scripts
A script is a page of steps you run once. An application is a small machine: a door (main), a config (what changes per school), a domain layer (rules that must stay testable), and an edge (print, file, network, screen). Internships fail people who only have one 400-line file named final.py. This chapter is the blueprint every later library sits on.
Application blueprint
A program is folders, config and a main door.
You will be able to
- ✓ Separate config, rules and output
- ✓ Explain if __name__ == "__main__" as the program door
- ✓ Keep business rules in functions you can test
Scripts grow into applications
The first program you write is a script: top to bottom, print, done. That is correct for learning. It is wrong for anything a second person will run next month. The moment a fee rule, a file path and a print statement live in the same paragraph, you cannot change the school name without breaking the maths.
An application has layers. Config is data about this campus (name, GST rate, data folder). Domain functions are rules that take plain values and return plain values — late_fee(days), utilisation(used, seats). The edge is how the outside world talks to you: a terminal, a CSV, a pygame window, a web request. Libraries like pandas and pygame belong at the edge or in a dedicated data/game layer — never mixed into a print statement in the middle of a fee formula.
The main door
In real Python, a file can be imported as a module or run as a program. if __name__ == "__main__": is the door: “only do the startup work when a human launched this file.” Helpers stay importable. Tests import late_fee without launching a window or writing a file.
This browser sandbox cannot import files. The idea still holds: put reusable functions at the top, put the “run me” sequence at the bottom. That is the same split as main().
Shape of a real file (full Python)
# config.py — change per campus, not per function
SCHOOL = "RSIL Demo School"
GST_RATE = 18
# fees.py — pure rules, no print
def add_gst(amount):
return amount * (100 + GST_RATE) / 100
if __name__ == "__main__":
print("Bill", add_gst(100))
Config is data, not code sprinkled everywhere
A GST of 18 appearing in seven print lines will become 18 in four places and 12 in three after the next budget. One dict or one small config file is the professional habit. Environment variables (os.environ) are how production apps keep secrets off the disk; beginners start with a CONFIG dict.
Name keys after meaning: data_dir, max_seats, heat_c. Do not name them x1, x2. Tomorrow’s pandas pipeline will read the same names from a .env or a YAML file.
What you will not do in the sandbox
The lesson player runs a teaching interpreter: variables, if, loops, lists, dicts, functions. It cannot import pandas, open files, or start pygame. Every Advanced lesson therefore has two tracks: a real-library sample you copy onto a laptop with pip, and a list/dict analog you Run here so the idea is executable today.
That split is honest. Production Python is CPython plus packages. Learning the analog first means the library code will look like a shorter spelling of something you already understand.
Words that matter
- Module
- A .py file other files can import. Functions live here; side effects should not.
- Entry point
- The main door — typically if __name__ == "__main__".
- Pure function
- Same inputs → same output; no hidden file or network writes.
- Config
- Values that change per school or deploy, kept in one place.
Common mistakes
Avoid: One 500-line file that prints, calculates, and hard-codes the school name.
Do this: Config dict + rule functions + a short main that only wires them.
Avoid: Putting pygame or pandas imports inside a fee function.
Do this: Keep domain rules importable without opening a window or reading a spreadsheet.
On a full Python install — stdlib (sys, pathlib)
Real apps take a file path from the command line and resolve folders with pathlib so Windows and Linux both work. This is the first library you already have — the standard library.
No pip needed — Python ships this.
A tiny CLI: python app.py data/attendance.csv
Real library code (not run in this browser sandbox)
from pathlib import Path
import sys
def load_path():
if len(sys.argv) < 2:
return Path("data") / "attendance.csv"
return Path(sys.argv[1])
if __name__ == "__main__":
path = load_path()
print("Would read", path.resolve())
Example program — Config + a pure rule + a short main
Change GST in CONFIG only. The function stays dumb and testable.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — One config, one rule
CONFIG has fine = 5. Write extra_days_fee(extra, fine) that returns extra * fine. Print the fee for 3 extra days so 15 appears.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.