Skip to content

Informatics Practices · Ch 3 — Brief Overview of Python

Python Keywords

3.2

Python Keywords

Every language — human or programming — has a small set of words whose meaning is fixed in advance. In Python, these are called keywords: reserved words that carry a specific, built-in meaning for the Python interpreter. The moment the interpreter encounters one of these words in a program, it knows exactly what structure or action the word signals. Because their meaning is already taken, keywords belong to the language itself and cannot be used by a programmer for any other purpose, such as naming a variable or a function.

Two rules govern how keywords must be written:

  • Each keyword is reserved for one specific job. if always signals a condition, for always signals a loop, import always brings in a module, and so on. The interpreter never lets you redefine what these words mean.
  • Python is a case-sensitive language, so every keyword must be typed exactly in its defined form. Writing TRUE, While or Import will not be recognised as the keywords True, while and import — a keyword changed in case is simply a different word to the interpreter.

The complete set of Python keywords is:

Falseclassfinallyisreturn
Nonecontinueforlambdatry
Truedeffromnonlocalwhile
anddelglobalnotwith
aselififoryield
assertelseimportpassbreak
exceptinraise

A useful observation while memorising this table: only three keywords begin with a capital letter — True, False and None. Every other keyword is written entirely in lowercase. This detail matters precisely because of case sensitivity: typing true instead of True produces an error, since lowercase true is not a keyword at all.

Watch out

A very common beginner mistake is trying to use a keyword as a name in a program — for example, writing class = 11 to store a class number. Since class is a reserved word, the interpreter rejects it. Whenever a name refuses to work as a variable, check it against the keyword table first. …

Table 3.1Python keywords
Falseclassfinallyisreturn
Nonecontinueforlambdatry
Truedeffromnonlocalwhile
anddelglobalnotwith
aselififoryield