Your First Program: From Blank File to Output

Your First Program: From Blank File to Output in Python: The REPL is fine for experiments, but real programs live in files.

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:

EditorWhy choose it
VS Code + Python extensionFree, lightweight, the most common choice
PyCharmDeepest Python-specific tooling, heavier
Any plain text editorWorks 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

  1. Running the command from the wrong folder. python hello.py only works if hello.py is in your current directory. No such file or directory almost always means you are one folder away — use cd to move, or pass the full path.
  2. Saving the file as hello.py.txt. Windows hides known extensions, so a file that looks like hello.py may really be hello.py.txt. Enable file extensions in Explorer to see the truth.
  3. Typing the code into the REPL and expecting a file. Anything typed at the >>> prompt vanishes when you close it.
  4. Capitalising print. Python is case-sensitive: Print("hi") raises NameError: name 'Print' is not defined.
  5. 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_case ending in .py — my_script.py, not My Script.py. Spaces make terminal commands awkward and hyphens break importing.
  • Never name a file after a module you use. A file called random.py will shadow the real random module 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

  1. Hello, You: Create hello.py that prints your name, save it, and run it with python hello.py.
  2. Break It Deliberately: Change print to Print and run the file. Read the NameError closely — note that it tells you the line number. Then fix it.
  3. Two Statements: Add a second print() line that outputs a calculation such as print(365 * 24). Confirm both lines appear in order, proving Python runs top to bottom.