Reserved Words and Keywords in Python
Exit the Python interpreter by typing quit() at the >>> prompt—the parentheses are essential.
The Exact Exit Command
Python is a formal language, so it does not interpret ordinary-language requests as commands. At the >>> prompt, the specific command for exiting the interpreter is quit(). The parentheses are essential because they are part of the command that executes the quit function.
Parentheses as the Action
The word quit identifies the quit function, but the source material emphasizes that quit() is the required exit command and that the parentheses are essential. In this context, the parentheses mark the form that executes the function rather than merely writing its name. Therefore, when the goal is to leave the interpreter, type the complete form quit().
Two Different Error Stages
Python can reject an incorrect attempt for different reasons. A NameError occurs when Python encounters an undefined name. A SyntaxError occurs when a reserved word is used with incorrect syntax. These errors therefore point to different problems: NameError concerns a name that Python cannot find, while SyntaxError concerns code that does not follow the required language structure.
| Situation | Python's response | What the error tells you |
|---|---|---|
| An undefined name is used | NameError | The name is not defined |
| A reserved word is used with incorrect syntax | SyntaxError | The code does not follow the required syntax |
The source-based distinction between NameError and SyntaxError
This distinction helps you reason about how Python processes input. When the problem is syntax, Python cannot accept the structure as valid code. When the problem is an undefined name, Python has encountered a name but cannot find a definition for it. Recognizing which error appears gives you a useful first clue during debugging.
Tracing Incorrect Exit Attempts
Diagnosing the error category
Compare two incorrect ideas: using an undefined name as though it were an exit command, and using the reserved word if without the syntax Python requires.
- 1First attempt
If the attempted command is an undefined name, Python reports
NameErrorbecause the name has not been defined. - 2Second attempt
If if is used incorrectly, Python reports
SyntaxErrorbecause if is a reserved word and the attempted form does not satisfy Python's syntax. - 3Correct form
At the >>> prompt, use
quit()with its parentheses. This is the specific exit command identified by the source material.
The error category depends on the kind of mistake: undefined name produces NameError, while incorrect use of a reserved word produces SyntaxError.
Mistakes to Diagnose
Common mistake Trying to exit with a natural-language request
What it looks likePlease exit the interpreter
Why it failsPython is a formal language and requires specific syntax rather than an ordinary-language command.
Do this insteadAt the >>> prompt, enter quit().Common mistake Leaving out the parentheses
What it looks likequit
Why it failsThe source identifies
quit()with parentheses as the required exit form and states that the parentheses are essential.Do this insteadType the complete command quit().Common mistake Treating every error as the same kind of problem
What it looks likeAssuming NameError and SyntaxError both mean that Python cannot understand the command for the same reason
Why it failsNameErrorindicates an undefined name, whereasSyntaxErrorindicates incorrect syntax involving a reserved word.Do this insteadUse the error category to identify whether the problem is an undefined name or invalid syntax.
Common mistake Assuming if behaves like an ordinary undefined name
What it looks likeUsing if incorrectly and expecting NameError
Why it failsif is a reserved word, so incorrect use produces
SyntaxError.Do this insteadRecognize reserved-word mistakes as syntax problems.
Check Your Diagnosis
At the >>> prompt, decide which response category matches each situation: an undefined name is entered, if is used with incorrect syntax, or quit() is entered exactly as required. For each situation, name the expected result and explain whether the issue concerns a name, syntax, or the exit command.
Hints
- An undefined name leads to
NameError. - Incorrect use of the reserved word if leads to
SyntaxError. - The complete exit command is
quit(), including the parentheses.
What do you think happens?
Before checking the explanation, predict the error category for an undefined name and for incorrectly used if.
Reveal answer
The undefined name produces NameError, while incorrectly used if produces SyntaxError.
Python reports NameError for an undefined name. It reports SyntaxError when a reserved word is used with incorrect syntax.
Key Takeaways
- 1Use
quit()at the >>> prompt to exit the Python interpreter. - 2The parentheses in
quit()are essential to the required exit command. - 3
NameErrormeans that an undefined name was used. - 4
SyntaxErrormeans that a reserved word was used with incorrect syntax. - 5Identifying the error category helps you debug by separating name problems from syntax problems.
At a glance
- The exact command for exiting the Python interpreter is
quit()at the >>> prompt. - The parentheses are essential because
quit()is the required form for executing the exit command. - An undefined name produces
NameError. - Incorrect syntax involving a reserved word such as if produces
SyntaxError. - The error type provides a debugging clue about whether the problem is a name or the structure of the code.