Concept Explanation
The REPL is fine for experiments, but real programs live in files. A Python file is plain text with a .py extension, and running it executes the lines from top to bottom.
The three steps
1. Create the file. Make a file called hello.py containing one line:
print("Hello, world!")
2. Run it. In the terminal, from the folder containing the file:
python hello.py
3. Read the output.
Hello, world!
That is the entire cycle: write, run, read. Every Python program you ever build is this loop repeated.
What Python does with your file
There is no separate compile step to run by hand. Python reads the file, translates it to bytecode, and executes that on the Python Virtual Machine. You may notice a __pycache__ folder appear — that is cached bytecode, safe to ignore and safe to delete.
No boilerplate required
Compare Python's complete program to Java's:
print("Hello, world!")
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Python has no mandatory class, no method wrapper, no type declarations. Statements at the top level of a file simply run. This is why Python is so often recommended as a first language.
Choosing an editor
You can use any text editor, but a proper one pays for itself immediately:
| Editor | Why choose it |
|---|---|
| VS Code + Python extension | Free, lightweight, the most common choice |
| PyCharm | Deepest Python-specific tooling, heavier |
| Any plain text editor | Works fine, but no autocomplete or error hints |
What actually matters is that your editor shows line numbers (error messages reference them) and highlights indentation, because in Python indentation is syntax.
Common Mistakes
- Running the command from the wrong folder.
python hello.pyonly works ifhello.pyis in your current directory.No such file or directoryalmost always means you are one folder away — usecdto move, or pass the full path. - Saving the file as
hello.py.txt. Windows hides known extensions, so a file that looks likehello.pymay really behello.py.txt. Enable file extensions in Explorer to see the truth. - Typing the code into the REPL and expecting a file. Anything typed at the
>>>prompt vanishes when you close it. - Capitalising
print. Python is case-sensitive:Print("hi")raisesNameError: name 'Print' is not defined. - Forgetting to save before running. Python reads the file from disk, not from your editor's unsaved buffer. Running an unsaved change executes the old version.
Best Practices
- Keep a scratch folder for practice files so your experiments don't scatter across your Desktop.
- Name files in
snake_caseending in.py—my_script.py, notMy Script.py. Spaces make terminal commands awkward and hyphens break importing. - Never name a file after a module you use. A file called
random.pywill shadow the realrandommodule and produce baffling errors. - Run your code often — after every few lines, not once at the end. Small changes make failures easy to locate.
- Read the last line of an error message first. It names the actual problem; the lines above are just the path Python took to get there.
Practice Exercises
- Hello, You: Create
hello.pythat prints your name, save it, and run it withpython hello.py. - Break It Deliberately: Change
printtoPrintand run the file. Read theNameErrorclosely — note that it tells you the line number. Then fix it. - Two Statements: Add a second
print()line that outputs a calculation such asprint(365 * 24). Confirm both lines appear in order, proving Python runs top to bottom.