Skip to content

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.

Beginner 10 min read·Lesson 21 of 306 in Python Programming

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.

PurposeKeywords
ValuesTrue, False, None
Logicand, or, not, is, in
Decisionsif, elif, else
Loopsfor, while, break, continue
Functionsdef, return, lambda, yield, global, nonlocal
Classesclass
Errorstry, except, finally, raise, assert
Modulesimport, from, as
Otherwith, pass, del, async, await

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

Python
import keyword
keyword.iskeyword("word")
keyword.kwlist
  • 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?

Python
import keyword
print(keyword.iskeyword("for"))
print(keyword.iskeyword("total"))
print(keyword.iskeyword("True"))
Output
True
False
True

How many keywords?

Python
import keyword
print(len(keyword.kwlist))
Output
35

More examples

Checking a list of proposed names

Python
import keyword
proposed = ["class", "class_name", "lambda", "price", "None", "none"]
for name in proposed:
if keyword.iskeyword(name):
print(f"{name!r}: reserved, choose another name")
else:
print(f"{name!r}: OK")
Output
'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:

TextOutput
'class': reserved, choose another name
'class_name': OK
'lambda': reserved, choose another name
'price': OK
'None': reserved, choose another name
'none': OK

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

class = "10-B"

Prefer

class_name = "10-B"

class is reserved, so this is a SyntaxError. Add a word or a trailing underscore.

Capitalising True or None incorrectly

Avoid

is_open = true

Prefer

is_open = True

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

  1. 1
    Print every keyword that starts with the letter e.
    Show hint

    Loop over keyword.kwlist and use startswith().

  2. 2
    Explain 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?
Practise this topic