Informatics Practices · Ch 3 — Brief Overview of Python
Python Keywords
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.
ifalways signals a condition,foralways signals a loop,importalways 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,WhileorImportwill not be recognised as the keywordsTrue,whileandimport— a keyword changed in case is simply a different word to the interpreter.
The complete set of Python keywords is:
| False | class | finally | is | return |
| None | continue | for | lambda | try |
| True | def | from | nonlocal | while |
| and | del | global | not | with |
| as | elif | if | or | yield |
| assert | else | import | pass | break |
| except | in | raise |
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.
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. …
| False | class | finally | is | return |
|---|---|---|---|---|
| None | continue | for | lambda | try |
| True | def | from | nonlocal | while |
| and | del | global | not | with |
| as | elif | if | or | yield |