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.

🐍Code Cell
1a = 10
Output
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:

🐍Code Cell
1cash = 10
2total123 = 50
3_java = "Python"
Output
All identifiers are valid.

Invalid Characters

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

🐍Code Cell
1ca$h = 20
Output
SyntaxError

Rule 2: Identifier Cannot Start with a Digit

An identifier cannot begin with a number.

Invalid Example:

🐍Code Cell
1123total = 100
Output
SyntaxError

Valid Example

🐍Code Cell
1total123 = 100
Output
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.

🐍Code Cell
1total = 10
2TOTAL = 999
3 
4print(total)
5print(TOTAL)
Output
10
999

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.

🐍Code Cell
1def = 10
Output
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.

🐍Code Cell
1price$ = 500
Output
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.

🐍Code Cell
1_private = 100
Output
Single underscore identifier

Double Underscore (__name)

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

🐍Code Cell
1__strong_private = 200
Output
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.

🐍Code Cell
1__init__
2__str__
3__name__
Output
Special Python identifiers

Examples of Identifiers

Variable Identifier

🐍Code Cell
1age = 25
Output
Variable identifier

Function Identifier

🐍Code Cell
1def display():
2 print("Hello")
Output
Hello

Class Identifier

🐍Code Cell
1class Student:
2 pass
Output
Student class created

Best Practices

Always use meaningful names for identifiers.

Good Examples:

🐍Code Cell
1student_name = "Rahul"
2total_marks = 450
3user_age = 20
Output
Meaningful identifiers

Poor Naming Examples

🐍Code Cell
1a = "Rahul"
2b = 450
Output
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
📝 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