Module 2 · Python Syntax and Fundamentals — topic 7 of 14
Keywords
Keywords are reserved words such as if, for, def and True that have special meaning. Learn the full list, what each group does, and why they can’t be used as names.
Learning objectives
- List Python’s keywords and group them by purpose
- Check whether a word is a keyword
- Avoid using keywords as variable names
Definition
Keywords are reserved words such as if, for, def and True that have special meaning. Learn the full list, what each group does, and why they can’t be used as names.
Introduction
Keywords are words that Python has reserved for its own grammar. if starts a decision, for starts a loop, def defines a function. Because they have fixed meanings, you can’t use them as names for your own variables or functions.
Why it matters
Recognising keywords lets you read the structure of any program: they are the signposts of decisions, loops and definitions. Knowing the list also explains confusing errors such as a SyntaxError when you name a variable class.
Detailed explanation
Python 3 has 35 keywords. They are all lower-case except True, False and None.
Soft keywords such as match, case and type act as keywords only in specific places, so they can still be used as ordinary names elsewhere. Avoid that to keep code clear.
The keyword module lets a program list or check keywords.
Syntax
- iskeyword() returns True for a reserved word.
- kwlist is a list of every keyword.
- Both are handy when choosing names programmatically.
Code examples
Is it a keyword?
True False True
How many keywords?
35
More examples
Checking a list of proposed names
'class': reserved, choose another name 'class_name': OK 'lambda': reserved, choose another name 'price': OK 'None': reserved, choose another name 'none': OK
Step-by-step explanation
- proposed holds candidate variable names as strings.
- keyword.iskeyword checks each one against Python’s reserved list.
- {name!r} shows the name with quotes so it is clear which word is meant.
- none (lower-case) is allowed because keywords are case-sensitive, but it is confusing and best avoided.
Expected output
Running “Checking a list of proposed names” prints exactly:
Practical applications
- Code generators that turn database column names into Python attributes check for keywords to avoid broken code.
- Editors colour keywords differently so the structure of a program stands out.
- Libraries add a trailing underscore, such as class_, when an argument name would clash with a keyword.
Best practices
- Learn to recognise keyword colouring in your editor.
- Use a trailing underscore (class_) when you must echo a keyword.
- Avoid names that differ from keywords only by case.
Common mistakes
Using a keyword as a variable
Avoid
Prefer
class is reserved, so this is a SyntaxError. Add a word or a trailing underscore.
Capitalising True or None incorrectly
Avoid
Prefer
Only True, False and None are capitalised; true is just an undefined name.
Key takeaways
- Keywords are reserved words with fixed meaning; there are 35.
- True, False and None are the only capitalised keywords.
- Use keyword.iskeyword() to check a word.
Try it yourself
- 1Print every keyword that starts with the letter e.
Show hint
Loop over keyword.kwlist and use startswith().
- 2Explain why import = 5 fails but imports = 5 works.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhich of these is a Python keyword?