Concepts / Debugging Python Code

Debugging Python Code

How This (does not) WorkThis program does not work! Python says there is a syntax error which means that the script does not satisfy the structure that Python expects to see. When we observe the error given by Python, it also tells us the place where it detected the error as well. So we start debugging our program from that line.

  • 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.

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

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.

type and press EnterlaunchesOS terminal$ or C:\>pythoncommand enteredPython interpreter>>>
What changes in the command environment when you enter the Python command in the operating system terminal?
  1. Open the operating system terminal.
  2. At the operating system prompt, type python.
  3. Press Enter.
  4. Confirm that the prompt changes to >>>.
  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.

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

One Statement Through the REPL

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

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

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

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

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.

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

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

  • Typing Python code at the operating system prompt

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

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

  • Treating >>> as an operating system prompt

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

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

  • Ignoring the line named by a syntax error

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

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

  • Expecting the Python prompt to disappear permanently after one statement

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

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

Key Takeaways

  • 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.