Nearby lessons

12 of 159

Python - Identifiers

📌 What You Will Learn
  • Understand what identifiers are in Python
  • Learn the rules for naming identifiers
  • Know which names are allowed and which are not

Introduction

An Identifier is a name used to identify different elements in a Python program.

Identifiers are used to name:

  • Variables
  • Functions
  • Classes
  • Modules
  • Objects

Choosing meaningful identifiers makes programs easier to read and understand.

Example of an Identifier

In the following example, a is an identifier.

Loading editor…

Rules for Naming Identifiers

Python has some rules for creating valid identifiers.

Rule 1: Allowed Characters

An identifier can contain only:

  • Alphabets (A-Z, a-z)
  • Digits (0-9)
  • Underscore (_)

Valid Examples:

Loading editor…

Invalid Characters

Special characters such as $, @, #, and % are not allowed.

Loading editor…

Rule 2: Identifier Cannot Start with a Digit

An identifier cannot begin with a number.

Invalid Example:

Loading editor…

Valid Example

Loading editor…

Rule 3: Identifiers are Case Sensitive

Python is a case-sensitive language.

This means identifiers with different letter cases are treated as different names.

Loading editor…

Rule 4: Reserved Words Cannot Be Used

Python keywords (reserved words) like if, def, class, and for cannot be used as identifiers.

For the full list of reserved words, visit Python Reserved Words.

Loading editor…

Rule 5: No Length Limit

Python does not limit the length of an identifier.

However, very long names should be avoided because they reduce readability.

Rule 6: Dollar Symbol is Not Allowed

The dollar symbol ($) is not allowed in Python identifiers.

Loading editor…

Valid and Invalid Identifier Examples

Identifier Status
total123 Valid
java2share Valid
abc_abc Valid
123total Invalid
ca$h Invalid
def Invalid
if Invalid

Special Meaning of Underscore (_)

Identifiers with underscores have special meanings in Python.

Single Underscore (_name)

An identifier starting with a single underscore indicates that it is intended for internal use.

Loading editor…

Double Underscore (__name)

An identifier starting with two underscores is used for stronger name protection inside classes.

Loading editor…

Double Underscore on Both Sides (__name__)

Identifiers that start and end with double underscores are special Python names, often called magic methods.

Loading editor…

Examples of Identifiers

Variable Identifier

Loading editor…

Function Identifier

Loading editor…

Class Identifier

Loading editor…

Best Practices

Always use meaningful names for identifiers.

Good Examples:

Loading editor…

Poor Naming Examples

Loading editor…

Benefits of Meaningful Identifiers

  • Improves code readability.
  • Makes programs easier to maintain.
  • Follows clean coding practices.
  • Helps other developers understand the code.

Key Points

  • Identifiers are names used for variables, functions, classes, modules, and objects.
  • Only alphabets, digits, and underscores are allowed.
  • Identifiers cannot start with a digit.
  • Python is case-sensitive.
  • Reserved words cannot be used as identifiers.
  • The dollar symbol ($) is not allowed.
  • Use meaningful names for better readability.

Quick Summary

Rule Description
Allowed Characters Letters, digits, and underscore (_)
Starting Character Cannot start with a digit
Case Sensitive Yes
Reserved Words Cannot be used
Dollar Symbol Not allowed
Length Limit No limit
📝 Key Takeaways
  • Identifiers are names used for variables, functions, classes, etc.
  • Can contain letters, digits, and underscores — cannot start with a digit
  • Cannot use Python reserved words as identifiers
  • Python is case-sensitive — Name and name are different
  • Use meaningful names like student_name, not x or y

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.