To see a list of Python keywords (reserved words), use the keyword

Money and Business

A list of Python keywords (reserved words) can be found in the keyword module of the standard library.

Keywords (reserved words) cannot be used as names (identifiers) for variable names, function names, class names, etc.

The following information is provided here.

  • Get a list of Python keywords (reserved words):keyword.kwlist
  • Check if the string is a keyword (reserved word):keyword.iskeyword()
  • The difference between keywords and reserved words

As mentioned in the last section, keywords and reserved words are strictly different concepts.

The following sample code uses Python 3.7.3. Note that the keywords (reserved words) may differ depending on the version.

Get a list of Python keywords (reserved words): keyword.kwlist

The keyword.kwlist contains a list of keywords (reserved words) in Python.

In the following example, pprint is used to make the output easier to read.

import keyword
import pprint

print(type(keyword.kwlist))
# <class 'list'>

print(len(keyword.kwlist))
# 35

pprint.pprint(keyword.kwlist, compact=True)
# ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break',
#  'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for',
#  'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not',
#  'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

The elements of the list are strings.

print(keyword.kwlist[0])
# False

print(type(keyword.kwlist[0]))
# <class 'str'>

If you try to use these names as identifiers (variable names, function names, class names, etc.), you will get an error.

# True = 100
# SyntaxError: can't assign to keyword

Check if the string is a keyword (reserved word): keyword.iskeyword()

You can check if a string is a keyword (reserved word) by using keyword.iskeyword().

When you specify the string you want to check as an argument, it returns true if it is a keyword, and false if it is not.

print(keyword.iskeyword('None'))
# True

print(keyword.iskeyword('none'))
# False

The difference between keywords and reserved words

Although we have been using them without making any distinction, strictly speaking, keywords and reserved words are two different concepts.

  • Keywords: words with special meaning in the language specification
  • Reserved words: words that satisfy the rules for identifiers as strings but cannot be used as identifiers.

See the following links for more details, including examples such as goto is a reserved word but not a keyword in Java.

In a computer language, a reserved word (also known as a reserved identifier) is a word that cannot be used as an identifier, such as the name of a variable, function, or label – it is “reserved from use”. This is a syntactic definition, and a reserved word may have no user-define meaning.
A closely related and often conflated notion is a keyword, which is a word with special meaning in a particular context. This is a semantic definition. By contrast, names in a standard library but not built into the language are not considered reserved words or keywords. The terms “reserved word” and “keyword” are often used interchangeably – one may say that a reserved word is “reserved for use as a keyword” – and formal use varies from language to language; for this article we distinguish as above.
Reserved word – Wikipedia

Keywords have a special meaning in a language, and are part of the syntax.
Reserved words are words that cannot be used as identifiers (variables, functions, etc.), because they are reserved by the language.
language agnostic – What is the difference between “keyword” and “reserved word”? – Stack Overflow

In Python (at least as of Python 3.7) all keywords are reserved words and there are no other reserved words besides keywords, so it is safe to use them without making any distinction.

See also the following article for names that can be used as identifiers.

Copied title and URL