Concepts / Running Python in Interactive Mode

Running Python in Interactive Mode

The standard procedure for writing Python programs consists of four steps: open an editor, type code, save as a .py file, and run using the python command.

  • Programming

From Editor to Output

Running a Python program is not one isolated action. The program passes through a sequence of states: you open an editor, type code, save that code as a file, and run the file with the python command. Understanding this sequence makes the workflow easier to follow and makes errors easier to diagnose.

thenthenthenproducesOpen editorType codeSave .py fileRun python commandOutput
What happens next as code moves from an editor to a running Python program?

The Four-Step Procedure

  1. Open an editor where you can write the program.
  2. Type the Python code into the editor.
  3. Save the program as a file with the .py extension.
  4. Run the saved file using the python command.

The first two steps create the program in the editor. At this point, the code exists in the editor's memory. The third step writes the code to disk as a persistent file. The fourth step tells the Python interpreter to read that saved file, execute its instructions, and produce output.

The .py file extension is the standard convention for Python scripts. It identifies the file as containing Python code.

savepython commandproducesEditor memoryCode being editedSaved .py filePersistent codeProgram executingInterpreter reads fileOutputProduced result
How does a Python program change as it moves from editing to execution?

A Greeting Program Walkthrough

Greeting Someone by Name

Trace the workflow for a program that greets someone by name.

Open and type: Open an editor and type the code for the greeting program. The code exists in the editor's memory while you work on it.

Save: Save the code as a file whose name ends with .py. The program now exists as a persistent file on disk.

Run: Use the python command to run the saved file. The interpreter reads the file and executes its instructions.

Observe output: The executing program produces the greeting as output.

The complete workflow moves the greeting from editor memory to a saved .py file, then to execution and produced output.

This example shows why the save step belongs between editing and running. If the greeting is changed in the editor but the changed version is not saved, running the file uses the version that is already on disk rather than the unsaved editor content.

What do you think happens?

You change the greeting in the editor and immediately run the file without saving. Which version does the interpreter read?

  • The newest code visible in the editor
  • The saved version of the file on disk
  • Both versions at the same time
Reveal answer

Answer: The saved version of the file on disk

The python command causes the interpreter to read the file that was saved to disk. Changes that remain only in editor memory have not yet become part of that file.

Names Commands and Locations

Successful execution depends on several details matching. The saved file needs the .py extension, the command needs to be typed correctly, and the command needs to be run from the directory containing the saved file. A mismatch at any of these points can prevent the intended program from running.

runspythonInterpreter commandscript.pySaved Python file
What parts of a Python run command must identify the interpreter and the saved script?

Workflow Mistakes

  • Forgetting to save before running

    The python command reads the saved file on disk, not changes that remain only in editor memory.

    Fix: Save the file before using the python command.

  • Using the wrong file extension

    The .py extension is the standard convention that identifies a Python script.

    Fix: Save the program with a filename ending in .py.

  • Running from the wrong directory

    The command must be able to identify the saved file being run.

    Fix: Run the command from the directory containing the saved file.

  • Mistyping the python command

    The interpreter command and the saved file must be specified correctly.

    Fix: Check the command spelling and confirm that it identifies the saved .py file.

supports runningsave changesuse .pyuse file directorycorrect commandSaved .py fileCorrect command anddirectoryUnsaved changesEditor differs from diskWrong extensionNot saved as .pyWrong directoryFile location does notmatchMistyped commandCommand or filename isincorrect
What changes when the file is unsaved, has the wrong extension, is in another directory, or is run with an incorrect command?

A Reliable Pre-Run Check

  1. Confirm that the intended code is present in the editor.
  2. Save the program before attempting to run it.
  3. Confirm that the saved filename ends in .py.
  4. Check that the command uses the correct python command.
  5. Make sure the command is run from the directory containing the saved file.
  6. If the expected result does not appear, check each workflow state in order.
EASY

A program is visible in the editor, but running it does not use the latest changes. Identify the missing workflow transition and explain the correction.

Hints
  • Compare the code in the editor with the code saved on disk.
  • Check whether the latest changes were saved before the python command was used.

Workflow Summary

  1. The standard procedure is to open an editor, type code, save a .py file, and run it with the python command.
  2. Code first exists in editor memory, where unsaved changes can be lost if the computer crashes.
  3. Saving writes the code to disk as a persistent file.
  4. Running the python command causes the interpreter to read the saved file, execute its instructions, and produce output.
  5. When execution fails or uses old code, check saving, the .py extension, the directory, the filename, and the command syntax.

Key Takeaways

  • Create and run a Python program by opening an editor, typing code, saving a .py file, and using the python command.
  • Recognize the transition from temporary editor memory to a persistent file on disk.
  • Remember that the interpreter reads and executes the saved file.
  • Troubleshoot by checking the save status, file extension, directory, filename, and command spelling.