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.
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.
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.
- 1Open the operating system terminal.
- 2At the operating system prompt, type python.
- 3Press Enter.
- 4Confirm that the prompt changes to >>>.
- 5Enter 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.
One Statement Through the REPL
What should you observe when you enter the source example at the Python prompt?
- 1Enter the statement
At the >>> prompt, enter print "Hello World" and press Enter.
- 2Observe the output
Python executes the statement and displays Hello World on the next line.
- 3Watch 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.
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.
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 likeEntering a Python statement while the prompt still shows $ or C:\>.
Why it failsThe operating system terminal understands operating system commands such as cd, ls, or dir; it does not understand Python code.
Do this insteadType python first, press Enter, and wait for the >>> prompt before entering Python statements.
Common mistake Treating >>> as an operating system prompt
What it looks likeTrying to use an operating system command after the Python prompt appears.
Why it failsThe >>> prompt signals that you are inside the Python interpreter, where Python code is the native language.
Do this insteadUse 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 likeSeeing a syntax error but beginning the investigation at an unrelated part of the script.
Why it failsPython reports the place where it detected the structural problem, giving you a starting point for debugging.
Do this insteadBegin 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 likeAssuming that the interpreter has stopped because >>> appears again after the output.
Why it failsThe returning prompt is the Loop part of the REPL cycle. It means Python is ready for another statement.
Do this insteadTreat 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.
Prompt Recognition Practice
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.
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
- 1The operating system prompt is shown as $ or C:\>; the Python interpreter prompt is >>>.
- 2Type python at the operating system terminal and press Enter to open the Python interpreter.
- 3At >>>, Python reads, evaluates, prints, and loops back to the prompt after each statement.
- 4The returning >>> prompt means Python is ready for another statement.
- 5For a syntax error, begin debugging at the location Python reports as the place where it detected the problem.
- 6Type
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.