Python Keywords

In the last lesson you learned Python Comments, where you saw how to leave notes in your code that Python ignores. Now we look at the opposite side. Python has a small set of words it never ignores. It has already claimed these for itself. These are called keywords. If you try to use one as your own name, Python stops you. Let us see why.

🤔 Why Keywords Matter

Say you want to store a value. The most natural name that comes to mind is class or for. You write for = 5 and run it. Instead of working, your program crashes before it even starts.

That is the pain. So let us name it clearly.

  • A name that feels perfect is sometimes off-limits, and you have no way to guess that ahead of time.
  • The error message does not always make it obvious why. It points near the wrong spot, so you waste time hunting.
  • The fix is tiny once you know the cause. You just pick a different name.

Keywords are words Python has reserved for its own grammar. So when one is blocked, you do not fight it. You rename and move on. Knowing the list ahead of time saves you from these confusing little errors.

🐍 What Keywords Are

A keyword is a word that Python already uses to mean something specific in its own rules. So think of words like if, while, def, and return. Each one tells Python to do a particular job. That is why Python will not let you point them at something else. If if could also be your variable, Python would not know whether you mean “make a decision” or “read my value”. So it keeps the word for itself.

Here is a real-world way to picture it. Imagine a parking lot. A few spots are painted with “Reserved for Manager”.

  • Anyone can park in the normal spots. Those are like your own variable names. You pick whatever you want.
  • The reserved spots are taken, no matter how much you want them. Those are the keywords.

So keywords are the reserved spots, and your names are the normal ones. The good news is the list is short and it almost never changes. Once you have seen it, you will recognize these words and naturally steer around them.

⛔ What Happens If You Use One

Let us trigger the error on purpose so you know exactly what it looks like. This code tries to use the keyword for as a variable name.

for = 5
print(for)

Python refuses to run this and reports a problem.

Output

File "main.py", line 1
for = 5
^
SyntaxError: invalid syntax

Notice it says SyntaxError. So let us read what that is telling us.

  • SyntaxError means Python could not even understand the grammar of your code. Nothing ran at all. It failed before the first line did anything.
  • The little arrow points near the spot where Python got confused, which is often the equals sign, not the word itself.
  • Because for is reserved for loops, Python expected loop code after it. Then it saw an equals sign, got confused, and stopped.

The same thing happens with any keyword. This tries class and if as names.

# ❌ Avoid: these are keywords, so they cannot be names
class = "Physics"
if = 10
# ✅ Good: pick a plain name that is not reserved
class_name = "Physics"
limit = 10

The pattern is always the same. So when a name that feels right gives you a SyntaxError, your first thought should be: is this a keyword?

📋 How to See the Full List

You do not have to memorize every keyword. Python can show you the whole list any time. So you never have to guess. There are two easy ways.

The first way uses the built-in keyword module. A module is just a file of ready-made tools that you bring into your program with import. This code imports it and prints the full list.

import keyword
print(keyword.kwlist)

Running it prints every keyword Python currently has.

Output

['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']

That kwlist is just a list of all the reserved words, ready for you to read. So if you ever forget one, you print this list and there it is.

If you only want to check a single word, the same module has a quick test called iskeyword. This asks whether "while" and "hello" are keywords.

import keyword
print(keyword.iskeyword("while"))
print(keyword.iskeyword("hello"))

The first is reserved and the second is a normal word. So you get one True and one False.

Output

True
False

So that is your quick safety check. Before you commit to a name, ask keyword.iskeyword("yourname") and let Python answer.

The second way needs no code at all. Open the Python prompt and type help("keywords"). Python prints the same list with a short header. That is handy when you just want a quick look without writing anything.

Tip

Keywords are case-sensitive. True is a keyword but true is not. None works while none does not. So watch the capital letters on True, False, and None.

🗂️ A Quick Tour by Job

You do not need to learn what each keyword does right now. Later lessons cover them properly. For now it just helps to see that they fall into small groups by the kind of job they do. When they are grouped, the list stops feeling like random words and starts making sense.

So here is each group with a one-line plain meaning and a tiny taste of how it looks.

Group Keywords Plain meaning Tiny example
Values True, False, None Fixed values for yes, no, and “nothing here” paid = True
Logic and, or, not, in, is Combine and compare values if age > 18 and ok:
Decisions if, elif, else Run different code depending on a condition if x: ... else: ...
Loops for, while, break, continue, pass Repeat work and control the loop for n in nums:
Functions and classes def, return, lambda, class Build reusable blocks and give back results def greet(): return "hi"
Imports import, from, as Bring in tools from other files from math import pi
Errors try, except, finally, raise Catch and handle problems without crashing try: ... except: ...
Scope and others global, nonlocal, del, with, yield, assert, async, await Manage where names live and a few special jobs with open(f) as file:

Do not worry about the details. The point right now is just to recognize these words. Then you know they are reserved, so you pick other names for your own values.

🌱 A Calm Word on Soft Keywords

There is one small twist worth knowing. A few words are soft keywords. A soft keyword only acts like a keyword in certain places, so Python still lets you use it as a name. These are match, case, type, and the single underscore _. So writing match = 5 actually runs, because Python only treats match as special inside a match statement. It works, but it is still better to avoid these as names so your code stays clear. You do not have to remember this now. Just know that not every special word is fully blocked.

⚠️ Common Mistakes

A few slip-ups come up again and again with keywords. So here is what to watch for.

  • Using a keyword as a variable name. Words like for, if, and class give a SyntaxError when you use them as names. A word like list = 5 does run, but it quietly hides a built-in, which is also best avoided. So pick a plain name instead.
  • Forgetting the capital letters. Writing true or none does not give you the keyword. Only True, False, and None work.
  • Thinking the error is in the wrong place. A keyword used as a name often points the arrow at the equals sign, not the word. So read the whole line, not just where the arrow lands.
  • Trying to memorize all of them. You do not need to. Use keyword.kwlist whenever you are unsure.
# ❌ Avoid: keyword as a name, wrong case for the value
return = 3
done = none
# ✅ Good: safe name, correct keyword case
result = 3
done = None

✅ Best Practices

Small habits keep you clear of keyword trouble.

  • When a name gives a SyntaxError, suspect a keyword first and check with keyword.iskeyword("yourname").
  • Choose descriptive names like student_count or total_price. They almost never clash with keywords anyway.
  • Remember True, False, and None start with a capital letter, every time.
  • If a perfect name is reserved, add a small word to it. So class becomes class_name and for becomes for_each.

🧩 What You’ve Learned

✅ Keywords are reserved words that Python uses for its own grammar rules, so you cannot point them at your own values.

✅ You cannot use a keyword as a variable name, and trying gives a SyntaxError.

✅ You can see the full list with import keyword then print(keyword.kwlist), or by typing help("keywords").

keyword.iskeyword("word") tells you if a single word is reserved.

✅ Keywords fall into groups by job: values, logic, decisions, loops, functions and classes, imports, errors, and scope words.

✅ Soft keywords like match, case, type, and _ can be used as names, but it is still cleaner to avoid them.

True, False, and None are case-sensitive and must start with a capital letter.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What is a Python keyword?

    Why: Keywords are reserved by Python for its own rules, so you cannot use them as your own names.

  2. 2

    What happens when you run `for = 5`?

    Why: for is reserved for loops, so using it as a name breaks the grammar and Python raises a SyntaxError.

  3. 3

    How do you print the full list of Python keywords?

    Why: The built-in keyword module holds the list in keyword.kwlist, which you can print after importing it.

  4. 4

    Which of these is the correct keyword for 'nothing here'?

    Why: None is the keyword and it is case-sensitive, so it must start with a capital N.

🚀 What’s Next?

Now that you know which words are off-limits, you are ready to start creating names of your own and storing values in them.

Python Variables

Share & Connect