Table of Contents
What Will You Learn
In this lesson, you'll learn the difference between Python keywords and identifiers, and why they're fundamental to writing valid code. Discover
the reserved words you must avoid and the rules for naming variables, functions, and classes correctly.
Programming in Python starts with understanding the basic building blocks — keywords and identifiers. Keywords define the structure and rules of the language. They are fixed, reserved by the interpreter, and have special meaning. You cannot use them as names for variables or functions. Identifiers, on the other hand, are names that you create yourself. These are variables, functions, classes, modules — everything you name in code. Properly named identifiers make code readable and easy to understand.
Clear understanding of the difference between keywords and identifiers helps prevent errors, improves readability, and aligns with best coding practices.
What are the Keywords in Python?
Keywords are reserved words built into the Python language. They serve special functions: defining conditions, loops, code structure, and program behavior. The number of keywords is fixed and updated with new versions of Python.
Python interprets each keyword as a command. For example, if
begins a condition, def
defines a function, and class
creates a class. These
words cannot be used as identifiers. Trying to assign a value to a variable named for
will result in a syntax error.
Examples of keywords:
if
,else
,elif
— conditional statementsfor
,while
,break
,continue
— loops and loop controldef
,return
,lambda
— functions and return valuesclass
,pass
,super
— working with classesTrue
,False
,None
— logical and special valuesimport
,from
,as
— working with modulesand
,or
,not
,is
,in
— logical operatorstry
,except
,finally
,raise
— exception handling
GettingList of All Python keywords
The list of reserved words depends on the Python version. To get the current list, use the keyword
module:
import keyword
print(keyword.kwlist)
This will print the up-to-date list of all keywords for the installed version of Python.
This is especially useful when creating variables: always check if the name matches any of the keywords. For example, a variable named input
is valid, but it shadows
the built-in input()
function, which can lead to logic errors.
Reserved Python keywords
Reserved keywords are a fixed set of words reserved in the Python language for internal use. They are part of the syntax and cannot be used as names for variables, functions, classes, or any other objects. Trying to override such a word will result in a syntax error.
Each reserved word has a specific purpose. These words cannot be used as identifiers.
For example:
def = 10 # ❌ SyntaxError
class = "Test" # ❌ SyntaxError
In this example, def
and class
are reserved words. Trying to assign values to them will result in a syntax error. The interpreter expects these words to
be used in their specific context, not as variable names.
Using reserved words out of context is a common mistake for beginners. Remember: these words are not for storing data, but for defining program logic.
What are Identifiers in Python?
Identifiers are names you assign to objects: variables, functions, classes, modules, etc. Unlike keywords, identifiers are created by the programmer. They are used to reference data stored in memory.
Each identifier must be unique within its scope. Well-chosen identifiers make code self-documenting — it's clear what a function or variable does even without comments.
Examples of identifiers:
user_name = "Alice"
MAX_RETRIES = 3
calculate_total()
user_name
, MAX_RETRIES
, and calculate_total
are identifiers. They are not reserved words but names created by the programmer
to represent specific data or functions.
Identifiers can be any combination of letters, digits, and underscores, but they must follow specific rules.
How Many Identifiers in Python?
Python places no limit on the number of identifiers you can use. This means you can create as many variables, functions, classes, modules, and other objects as you like, as long as their names are unique within their scope and follow syntax rules.
Example:
name = "Alice"
age = 30
def greet():
print("Hello")
Each name (name
, age
, greet
) is an identifier, and each points to an object in memory.
Key things to remember:
- Identifiers are created by the developer — they are not built into the language.
- In Python, everything is an object, and each object can have a name (identifier).
- Names of variables, functions, arguments, modules, methods, attributes — all of these are identifiers.
Although the language doesn't limit their number, there's an unwritten rule: the clearer the names, the better the program. Creating hundreds of meaningless or illogical identifiers leads to confusing, unreadable code.
Also, avoid overwriting built-in identifiers, for example:
list = [1, 2, 3] # ⛔ Overwrites the built-in type 'list'
This is technically allowed but considered bad practice because it breaks the behavior of standard functions.
So, you can have as many identifiers as you want, but the way you create them should be intentional: clear names, valid syntax, and no conflicts with keywords or built-in functions.
Rules for Naming an Identifier
Identifier naming follows strict rules. Violating these rules results in syntax errors or unpredictable code behavior. Beyond technical rules, there are also style guidelines that ensure readability and maintainability.
Basic rules:
- An identifier can contain letters (A–Z, a–z), digits (0–9), and underscores (
_
). - The name must begin with a letter or underscore, but not a digit.
- Identifiers are case-sensitive:
value
,Value
, andVALUE
are different names. - Keywords cannot be used as identifiers.
- Special characters (
@
,#
,$
,%
,-
, etc.) are not allowed in names. - Names should be meaningful and reflect their purpose:
total_price
,user_input
,MAX_LENGTH
.
Examples of valid names:
_name = "internal"
userAge = 30
MAX_LIMIT = 1000
In this example _name
, userAge
, and MAX_LIMIT
are valid identifiers. They follow the rules: start with a letter or underscore, contain only
letters, digits, and underscores, and are not keywords.
Examples of invalid names:
1st_name = "Alice" # ❌ Starts with a digit
user name = "Bob" # ❌ Contains a space
total$price = 100 # ❌ Contains a special character
In this example, 1st_name
, user name
, and total$price
are invalid identifiers. They violate the rules: starting with a digit, containing
spaces, or special characters.
Following these rules is crucial for writing correct and readable Python code. Using meaningful names makes your code easier to understand and maintain.
List of Python Keywords and Identifiers with Examples
Keywords and identifiers are often used together. For example, def
is a keyword, and greet_user
is an identifier. Distinguishing their roles helps the
interpreter understand what you're trying to do.
Below is a table with examples of keywords and identifiers:
Name | Type | Description | Example Usage |
---|---|---|---|
if |
Keyword | Conditional statement | if score > 90: |
for |
Keyword | Looping construct | for item in items: |
def |
Keyword | Function definition | def calculate_total(): |
True |
Keyword | Boolean value | is_admin = True |
import |
Keyword | Module import | import math |
print |
Identifier | Built-in output function | print("Hello") |
user_name |
Identifier | Variable for username | user_name = "Alice" |
value |
Identifier | Generic variable name | value = 42 |
_temp |
Identifier | Temporary (stylistically private) name | _temp = 5 |
MAX_USERS |
Identifier | Constant | MAX_USERS = 100 |
Common Beginner Mistakes
1. Using keywords as variable names
Beginners often mistakenly use reserved words as variable names, such as if = 10
or class = "Math"
. This results in a SyntaxError
because
these words have predefined meanings in the language. Solution: always check the list of keywords and avoid using them when creating identifiers.
2. Assigning values to built-in functions
Mistake: list = [1, 2, 3]
, str = "text"
. While technically allowed, this overrides access to the built-in list()
or
str()
functions. It's recommended not to use built-in function names for variables.
3. Breaking syntax rules when naming identifiers
Beginners try to name variables starting with a number (2name = "John"
), with spaces (user name = "Alice"
), or special characters (price$ = 100
). Such names are invalid.
4. Using very short or unclear names
Names like a
, b
, x1
do not explain the variable's purpose. It's better to use total_price
, user_id
,
max_value
— clear, convenient, and reliable.
5. Ignoring PEP 8 naming style
For example, mixing camelCase
, snake_case
, and ALL_CAPS
without logic. Constants should be in ALL_CAPS
, variables and
functions in snake_case
. Style matters for readability and teamwork.
Frequently Asked Questions
How many keywords in Python?
As of Python 3.10, there are 36 keywords, including if
, else
, while
, def
, import
,
class
, True
, False
, None
, try
, except
, and others. The list is not static: new versions of Python
may add more. For example, Python 3.9 introduced match
and case
for structural pattern matching. It’s important to check the latest list periodically.
In small projects, you rarely use all keywords, but it’s essential to know them to avoid conflicts when naming variables or functions.
How to see keywords in Python?
The easiest and most reliable way is to use the built-in keyword
module. It’s part of the standard library and allows you to get a full list of current keywords at
any time:
import keyword
print(keyword.kwlist)
This list is specific to your Python version. You can also check the number of keywords using len(keyword.kwlist)
. This is especially helpful for beginners to
avoid using reserved words as variable names. Many editors (such as VS Code or PyCharm) also highlight keywords automatically, helping you avoid mistakes.
What are the most commonly used Python keywords?
The most frequently used keywords in practice include:
if
,else
,elif
— for conditionalsfor
,while
,break
,continue
— for loopsdef
,return
— for functionsimport
,from
,as
— for importing modulesclass
— for defining classestry
,except
,raise
— for exception handlingTrue
,False
,None
— for boolean and special values
These keywords form the core syntax of any Python application. It’s important not only to memorize them but also to understand how they work together. Other keywords like
global
, yield
, nonlocal
, assert
are less commonly used but still important to learn as you advance.
How are keywords different from identifiers in Python?
Keywords are built-in language constructs. They are reserved and serve specific roles in the syntax: conditionals, loops, function and class definitions. For example,
if
, def
, return
, class
are keywords and cannot be used as names for variables or functions.
Identifiers are names created by the programmer. They are used to name variables, functions, classes, and other objects. Examples include user_name
,
total_price
, calculate_area
. The key difference: keywords are fixed and built-in, while
identifiers are user-defined and can be anything that follows naming rules.
Must Python identifiers start with a letter or underscore?
Yes. This is one of the core syntax rules in Python:
an identifier must start with a letter (a–z, A–Z) or an underscore (_
). It cannot start with a digit — doing so will result in a syntax error.
Valid examples: name
, _value
, user1
. Invalid examples: 1name
, @data
, price$
. Underscores are often
used for internal names (_private_var
) or temporary variables (_temp
). Also important: Python is case-sensitive, so value
and
Value
are two different identifiers. Following these rules is critical for writing correct Python code.