Concepts / Debugging Python Code

Debugging Python Code

The >>> prompt belongs to Python. The $ or C:\> prompt belongs to the operating system. The prompt is therefore a quick visual check: it tells you which environment will interpret what you type next.

  • Programming

Two Command Environments

Debugging often begins with a small experiment: enter one statement, observe what Python reports, and decide what to inspect next. Before you can use that feedback cycle, you must know which environment is receiving your command. An operating system terminal and the Python interpreter are separate environments with different prompts and different responsibilities.

$ or C:\>operating system promptcd, ls, diroperating system commands>>>Python interpreter promptPython statementsread and executed by Pythonhandleshandles
How can you tell whether a command will be handled by the operating system or by Python?
Key point

The >>> prompt belongs to Python. The $ or C:\> prompt belongs to the operating system. The prompt is therefore a quick visual check: it tells you which environment will interpret what you type next.

Entering Python

The transition into Python begins at the operating system terminal. First, open the terminal so its operating system prompt is ready to receive a command. Then type python and press Enter. The operating system launches the Python interpreter, and the prompt changes to >>>. That change signals that Python code, rather than an operating system command, can now be entered.

OS terminal$ or C:\>pythoncommand enteredPython interpreter>>>type and press Enterlaunches
What changes in the command environment when you enter the Python command in the operating system terminal?
  1. 1
    Open the operating system terminal.
  2. 2
    At the operating system prompt, type python.
  3. 3
    Press Enter.
  4. 4
    Confirm that the prompt changes to >>>.
  5. 5
    Enter Python statements only after the >>> prompt appears.

The REPL Feedback Cycle

The Python interpreter uses a cycle called the REPL: Read, Evaluate, Print, Loop. You type a statement at >>> and press Enter. Python reads the statement, evaluates or executes it, prints the result, and then shows >>> again. The returning prompt means the interpreter is ready for another statement.

ReadstatementEvaluateexecute statementPrintresult or error>>>next statementthenthenthennext statement
What happens after you type a Python statement at the interpreter prompt, and how does the result lead to the next statement?
Worked example

One Statement Through the REPL

What should you observe when you enter the source example at the Python prompt?

  1. 1
    Enter the statement

    At the >>> prompt, enter print "Hello World" and press Enter.

  2. 2
    Observe the output

    Python executes the statement and displays Hello World on the next line.

  3. 3
    Watch for the prompt

    The >>> prompt appears again, showing that the interpreter is ready for another statement.

Result

The interpreter gives immediate feedback: the statement is processed, its output appears, and the prompt returns.

python

Using Feedback to Debug

The REPL is useful for debugging because it shortens the distance between an attempted statement and the feedback about it. You can test small pieces of code, see the result immediately, and adjust your thinking without first writing and running a complete program.

When Python reports a syntax error, it is telling you that the script does not satisfy the structure Python expects. The error report also identifies the place where Python detected the problem. Begin debugging from that reported line. The reported location is the starting point for inspection, because it is where Python noticed that the structure was not valid.

Python scriptprogram structureReadPython examines the scriptSyntax errorstructure is not expectedReported lineplace Python detected theproblemexamined bydetects invalid structureidentifies location
How does Python move from reading a program to detecting a syntax error and identifying the line where it noticed the problem?
Best practice

When a syntax error appears, start at the line Python reports. Inspect that location and use the error message as the first clue about what part of the expected structure was not satisfied.

Mistakes at the Prompts

  • Common mistake

    Typing Python code at the operating system prompt

    What it looks like

    Entering a Python statement while the prompt still shows $ or C:\>.

    Why it fails

    The operating system terminal understands operating system commands such as cd, ls, or dir; it does not understand Python code.

    Do this instead

    Type python first, press Enter, and wait for the >>> prompt before entering Python statements.

  • Common mistake

    Treating >>> as an operating system prompt

    What it looks like

    Trying to use an operating system command after the Python prompt appears.

    Why it fails

    The >>> prompt signals that you are inside the Python interpreter, where Python code is the native language.

    Do this instead

    Use Python statements at >>>. Exit Python with exit() when you need to return to the operating system terminal.

  • Common mistake

    Ignoring the line named by a syntax error

    What it looks like

    Seeing a syntax error but beginning the investigation at an unrelated part of the script.

    Why it fails

    Python reports the place where it detected the structural problem, giving you a starting point for debugging.

    Do this instead

    Begin by inspecting the reported line and the structure Python expected there.

  • Common mistake

    Expecting the Python prompt to disappear permanently after one statement

    What it looks like

    Assuming that the interpreter has stopped because >>> appears again after the output.

    Why it fails

    The returning prompt is the Loop part of the REPL cycle. It means Python is ready for another statement.

    Do this instead

    Treat the returning >>> as immediate feedback that the interpreter is ready for the next experiment.

Leaving the Interpreter

When you finish experimenting, type exit() at the >>> prompt and press Enter. Python closes, and the operating system terminal prompt appears again. This return to $ or C:\> confirms that you have left the Python interpreter and can use operating system commands or launch Python again.

python

Prompt Recognition Practice

EASY

You open a terminal and see a $ prompt. You want to test a Python statement immediately. What should you do, and what prompt should appear before you enter the statement?

Hints
  • The operating system prompt is not the Python interpreter.
  • Use the command that launches Python, then look for the three greater-than symbols.
MEDIUM

A Python script produces a syntax error and Python identifies a line. What should be your first debugging action?

Hints
  • The reported location is where Python detected the problem.
  • Start your inspection there and consider whether the script has the structure Python expects.

Debugging Takeaways

  1. 1
    The operating system prompt is shown as $ or C:\>; the Python interpreter prompt is >>>.
  2. 2
    Type python at the operating system terminal and press Enter to open the Python interpreter.
  3. 3
    At >>>, Python reads, evaluates, prints, and loops back to the prompt after each statement.
  4. 4
    The returning >>> prompt means Python is ready for another statement.
  5. 5
    For a syntax error, begin debugging at the location Python reports as the place where it detected the problem.
  6. 6
    Type exit() at >>> to return to the operating system terminal.

At a glance

  • The operating system terminal and the Python interpreter are separate command environments.
  • The >>> prompt confirms that Python is ready to receive a Python statement.
  • The REPL provides immediate feedback through the Read, Evaluate, Print, and Loop cycle.
  • A syntax-error location is the place to begin inspecting a program's structure.
  • Use exit() to leave Python and return to the operating system terminal.

More concepts starting with D