bhylu.com

Advertisements

Python Keywords

Keywords belong to Python's grammar rather than a user-defined function library. They mark definitions, decisions, loops, exception handling, imports, and other statement forms.

Understand the idea

The keyword module lists reserved words for the interpreter you are running and separately exposes soft keywords in supported versions. Soft keywords have special meaning only in particular syntactic contexts. Use descriptive identifiers that avoid grammar collisions, and do not assume a list copied from one Python version describes every future interpreter.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

A worked example

Python

import keyword
print(keyword.iskeyword("class"))
print(keyword.iskeyword("booking"))
print("match" in keyword.softkwlist)

Read the result

The results are True, False, and True in the Python versions used by this course.

Try one change

Classify def, return, and yield by the kind of operation they express, then locate their full lessons.