DocsStation
DSA with Python

Python Keywords and Identifiers

By csestudy247 Updated December 2, 2026 6 min read
6 min read

Introduction to Python Keywords and Identifiers

Keywords and identifiers are fundamental concepts in Python programming. Every Python program uses keywords and identifiers to define instructions, variables, functions, classes, and other programming elements.

Keywords are reserved words that have predefined meanings in Python. They cannot be used as variable names or identifiers.

Identifiers are names given to variables, functions, classes, and other objects in a Python program.

In this tutorial, you will learn about Python keywords, identifiers, naming rules, valid and invalid identifiers, and practical examples.

1. What Are Keywords in Python?

Keywords are reserved words in Python that have special meanings and predefined purposes in the programming language.

These words are part of Python’s syntax and are used to perform specific operations.

For example:

if age >= 18:
    print("Eligible to vote")
else:
    print("Not eligible to vote")

In this example:

  • if is a Python keyword used for conditional execution.
  • else is a Python keyword used to define an alternative block.
  • print is a built-in function, not a keyword.

Python does not allow keywords to be used as variable names.

2. List of Python Keywords

Python provides several reserved keywords used for different programming operations.

Some commonly used Python keywords include:

KeywordDescription
ifExecutes code based on a condition
elseDefines an alternative condition block
elifChecks another condition
forCreates a for loop
whileCreates a while loop
breakTerminates a loop
continueSkips the current loop iteration
passActs as a placeholder
defDefines a function
returnReturns a value from a function
classDefines a class
importImports a module
fromImports specific components
asCreates an alias
tryStarts an exception-handling block
exceptHandles an exception
finallyDefines code that executes after try/except
raiseRaises an exception
andLogical AND operator
orLogical OR operator
notLogical NOT operator
inChecks membership
isChecks object identity
TrueBoolean true value
FalseBoolean false value
NoneRepresents the absence of a value
globalRefers to a global variable
nonlocalRefers to a variable in an enclosing function
lambdaCreates an anonymous function
withManages a context
yieldProduces a value from a generator
delDeletes a reference or item
assertChecks a condition
asyncDefines asynchronous code
awaitWaits for an asynchronous operation

Note: The exact keyword list can vary between Python versions.

3. How to Display Python Keywords

Python provides a built-in keyword module that allows you to access the list of reserved keywords.

Example

import keyword

print(keyword.kwlist)

Output:

The program displays the list of Python keywords supported by the installed Python version.

Checking Whether a Word Is a Keyword

You can use the iskeyword() function to check whether a particular word is a reserved keyword.

import keyword

print(keyword.iskeyword("if"))
print(keyword.iskeyword("else"))
print(keyword.iskeyword("name"))

Output:

True
True
False

In this example:

  • if is a keyword.
  • else is a keyword.
  • name is not a keyword.

4. What Are Identifiers in Python?

Identifiers are names used to identify variables, functions, classes, and other programming elements.

For example:

name = "Nitish"
age = 22

print(name)
print(age)

Output:

Nitish
22

In this example:

  • name is an identifier.
  • age is an identifier.

Identifiers help programmers refer to values and other elements throughout a program.

5. Rules for Naming Identifiers in Python

Python follows specific rules when creating identifiers.

Rule 1: An Identifier Cannot Start with a Digit

An identifier must start with a letter or an underscore.

Correct:

name1 = "Nitish"
age2 = 22

print(name1)
print(age2)

Incorrect:

1name = "Nitish"

This produces a SyntaxError because an identifier cannot begin with a number.

Rule 2: An Identifier Can Start with an Underscore

Python allows identifiers to begin with an underscore (_).

Example:

_ = "Nitish"

print(_)

Output:

Nitish

You can also use underscores within longer identifier names.

_student_name = "Rahul"

print(_student_name)

Rule 3: Special Characters Are Not Allowed

Identifiers can contain letters, digits, and underscores.

Special characters such as @, $, %, and ! cannot be used in ordinary identifier names.

Correct:

student_name = "Rahul"
student_age = 22

Incorrect:

student@name = "Rahul"
student$age = 22

Rule 4: Keywords Cannot Be Used as Identifiers

Python keywords have predefined meanings, so they cannot be used as variable names.

Incorrect:

if = 10
class = "Python"

Correct:

if_value = 10
class_name = "Python"

Rule 5: Spaces Are Not Allowed

An identifier cannot contain spaces.

Incorrect:

student name = "Rahul"

Correct:

student_name = "Rahul"

6. Valid and Invalid Identifiers

The following table shows examples of valid and invalid identifiers.

IdentifierValid / InvalidReason
nameValidStarts with a letter
name1ValidContains a digit after a letter
_nameValidStarts with underscore
student_ageValidContains an underscore
1nameInvalidStarts with a digit
student nameInvalidContains a space
student@nameInvalidContains a special character
classInvalidPython keyword
age2ValidStarts with a letter
total_marksValidUses letters and underscore

7. Python Identifiers Are Case-Sensitive

Python is a case-sensitive programming language.

This means uppercase and lowercase letters are treated differently when creating identifiers.

Example

name = "Nitish"
Name = "Rahul"
NAME = "Amit"

print(name)
print(Name)
print(NAME)

Output:

Nitish
Rahul
Amit

Here, name, Name, and NAME are treated as three different identifiers.

8. Difference Between Keywords and Identifiers

Keywords and identifiers have different purposes in Python.

KeywordsIdentifiers
Reserved words in PythonUser-defined names
Have predefined meaningsUsed to identify programming elements
Cannot be used as variable namesCan be used as variable names if naming rules are followed
Examples: if, else, forExamples: name, age, student_name

9. Practical Example of Keywords and Identifiers

Consider the following Python program:

# Student information

name = "Nitish"
age = 22

if age >= 18:
    print(name, "is eligible")
else:
    print(name, "is not eligible")

Output:

Nitish is eligible

In this program:

  • name and age are identifiers.
  • if and else are keywords.
  • print() is a built-in function.

This example demonstrates how keywords and identifiers work together in a Python program.

10. Practice Questions on Python Keywords and Identifiers

Test your understanding with these multiple-choice questions.

Question 1: What are Python keywords?

A. User-defined variable names

B. Reserved words with predefined meanings

C. Special characters

D. Data types only

Answer: B. Reserved words with predefined meanings.

Question 2: Which of the following is a valid identifier?

A. 1student

B. student name

C. student_name

D. class

Answer: C. student_name

Question 3: Which symbol can be used at the beginning of an identifier?

A. @

B. $

C. _

D. %

Answer: C. _

Question 4: Which Python module provides the list of reserved keywords?

A. math

B. random

C. keyword

D. datetime

Answer: C. keyword

Question 5: Are name and Name treated as different identifiers in Python?

A. Yes

B. No

C. Only in functions

D. Only in classes

Answer: A. Yes

Question 6: Which of the following cannot be used as a variable name?

A. student_age

B. age1

C. _name

D. for

Answer: D. for

Scroll to Top