Concepts / Reserved Words and Keywords in Python

Reserved Words and Keywords in Python

Exit the Python interpreter by typing quit() at the >>> prompt—the parentheses are essential.

  • Programming

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.

python
>>> promptready for inputquit()exit commandInterpreter exitedenterexecute
What changes when quit() is entered at the interpreter prompt?

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().

quitfunction name()call notationquit()required exit formcombined withcompletes
How do the parentheses change the command from naming quit to executing the required exit form?

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.

ifreserved wordunknown_nameundefined nameSyntaxErrorincorrect syntaxNameErrorname is undefinedused incorrectlyused
Why does Python raise SyntaxError for if used incorrectly but NameError for an undefined name?
SituationPython's responseWhat the error tells you
An undefined name is usedNameErrorThe name is not defined
A reserved word is used with incorrect syntaxSyntaxErrorThe 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

Worked example

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.

  1. 1
    First attempt

    If the attempted command is an undefined name, Python reports NameError because the name has not been defined.

  2. 2
    Second attempt

    If if is used incorrectly, Python reports SyntaxError because if is a reserved word and the attempted form does not satisfy Python's syntax.

  3. 3
    Correct form

    At the >>> prompt, use quit() with its parentheses. This is the specific exit command identified by the source material.

Result

The error category depends on the kind of mistake: undefined name produces NameError, while incorrect use of a reserved word produces SyntaxError.

Entered inputReserved word usedincorrectlySyntaxErrorUndefined name usedNameErrorquit()exit interpreterinspectyesotherwiseyesuse quit()
At what point does Python identify an incorrect exit attempt?

Mistakes to Diagnose

  • Common mistake

    Trying to exit with a natural-language request

    What it looks like

    Please exit the interpreter

    Why it fails

    Python is a formal language and requires specific syntax rather than an ordinary-language command.

    Do this instead
    At the >>> prompt, enter quit().
  • Common mistake

    Leaving out the parentheses

    What it looks like

    quit

    Why it fails

    The source identifies quit() with parentheses as the required exit form and states that the parentheses are essential.

    Do this instead
    Type the complete command quit().
  • Common mistake

    Treating every error as the same kind of problem

    What it looks like

    Assuming NameError and SyntaxError both mean that Python cannot understand the command for the same reason

    Why it fails

    NameError indicates an undefined name, whereas SyntaxError indicates incorrect syntax involving a reserved word.

    Do this instead

    Use 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 like

    Using if incorrectly and expecting NameError

    Why it fails

    if is a reserved word, so incorrect use produces SyntaxError.

    Do this instead

    Recognize reserved-word mistakes as syntax problems.

Check Your Diagnosis

EASY

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.

  • Both produce NameError

  • Both produce SyntaxError

  • The undefined name produces NameError, while incorrectly used if produces SyntaxError

Reveal answer
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

  1. 1
    Use quit() at the >>> prompt to exit the Python interpreter.
  2. 2
    The parentheses in quit() are essential to the required exit command.
  3. 3
    NameError means that an undefined name was used.
  4. 4
    SyntaxError means that a reserved word was used with incorrect syntax.
  5. 5
    Identifying 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.

Related concepts

More concepts starting with R