Nearby lessons

12 of 108

Python - Identifiers

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.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
a is an identifier.

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:

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
All identifiers are valid.

Invalid Characters

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
SyntaxError

Rule 2: Identifier Cannot Start with a Digit

An identifier cannot begin with a number.

Invalid Example:

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
SyntaxError

Valid Example

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Valid Identifier

Rule 3: Identifiers are Case Sensitive

Python is a case-sensitive language.

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
999

Rule 4: Reserved Words Cannot Be Used

Python keywords (reserved words) cannot be used as identifiers.

Invalid Example:

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
SyntaxError

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.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
SyntaxError

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.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Single underscore identifier

Double Underscore (__name)

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Double underscore identifier

Double Underscore on Both Sides (__name__)

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Special Python identifiers

Examples of Identifiers

Variable Identifier

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Variable identifier

Function Identifier

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Hello

Class Identifier

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Student class created

Best Practices

Always use meaningful names for identifiers.

Good Examples:

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Meaningful identifiers

Poor Naming Examples

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Avoid using unclear names.

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

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - Reserved Words