When you start writing Python programs, you will create variables to store information, functions to perform tasks, and classes to organize your code.
But how do you give names to these elements?
For example:
student_name = "Rahul"
student_age = 20
total_marks = 450
Here, student_name, student_age, and total_marks are names used to identify different variables. In Python, these names are called identifiers.
Understanding identifiers is important because choosing valid and meaningful names helps you write programs that are easier to read, understand, and maintain.
In this article, we will learn what identifiers are, the rules for naming them, valid and invalid examples, and the naming conventions followed by Python programmers.
1. What Is an Identifier in Python?
An identifier is a name used to identify a variable, function, class, or another named object in a Python program.
Consider this example:
name = "Amit"
age = 19
def display():
print(name, age)
In this program:
nameis an identifier for a variable.ageis an identifier for a variable.displayis an identifier for a function.
Identifiers help us refer to different elements of a program by name.
2. Why Are Identifiers Important?
Imagine writing a program that stores student information, calculates marks, and displays results.
You could use short names such as a, b, and c, but these names would not clearly explain what the variables represent.
Compare these examples.
Example 1: Unclear identifiers
a = "Rahul"
b = 85
c = 90
Example 2: Meaningful identifiers
student_name = "Rahul"
english_marks = 85
maths_marks = 90
The second example is easier to understand because each identifier describes its purpose.
Meaningful identifiers become especially useful when you work on large projects involving databases, websites, data science, and artificial intelligence.
3. Rules for Naming Identifiers in Python
Python follows specific rules when creating identifiers. Let's understand them one by one.
Rule 1: An identifier can contain letters, digits, and underscores
Valid examples:
student
student1
student_name
marks2026
These names are valid because they use permitted characters.
Rule 2: An identifier cannot start with a digit
Invalid examples:
1student = "Rahul"
2026marks = 90
Python does not allow identifiers to begin with a number.
Correct examples:
student1 = "Rahul"
marks2026 = 90
The digits can appear after the first character.
Rule 3: Spaces are not allowed
Invalid example:
student name = "Rahul"
Python treats the space as a separator, so this is not a valid variable assignment.
Correct example:
student_name = "Rahul"
Use an underscore when you want to separate words in a variable name.
Rule 4: Special characters are generally not allowed
Characters such as @, #, $, and % cannot be used within ordinary Python identifiers.
Invalid examples:
student@name = "Rahul"
total#marks = 450
price$ = 100
Correct examples:
student_name = "Rahul"
total_marks = 450
price = 100
The underscore _ is allowed and is commonly used to separate words.
Rule 5: Python keywords cannot be used as identifiers
Keywords have special meanings in Python, so they cannot be used as ordinary variable names.
Invalid examples:
if = 10
for = 20
class = "Python"
Correct examples:
number = 10
count = 20
course_name = "Python"
Choose a different name whenever a word is reserved by Python.
Rule 6: Identifiers are case-sensitive
Python treats uppercase and lowercase letters as different.
For example:
name = "Rahul"
Name = "Amit"
NAME = "Priya"
print(name)
print(Name)
print(NAME)
Output:
Rahul
Amit
Priya
The identifiers name, Name, and NAME refer to three different names.
For this reason, use capitalization consistently throughout your programs.
4. Valid and Invalid Identifiers
The following table summarizes common naming examples.
| Identifier | Valid or Invalid? | Reason |
|---|---|---|
student | Valid | Contains letters |
student1 | Valid | Digit appears after the first character |
student_name | Valid | Underscore is allowed |
_count | Valid | An identifier can start with an underscore |
totalMarks | Valid | Uses letters with no invalid characters |
1student | Invalid | Starts with a digit |
student name | Invalid | Contains a space |
student@name | Invalid | Contains @ |
if | Invalid | Reserved keyword |
total-marks | Invalid | Contains a hyphen |
Important: The rules above apply to ordinary Python identifiers. Python also supports Unicode letters, but beginners can generally use English letters, digits, and underscores for clarity.
5. Naming Conventions in Python
A naming rule determines whether an identifier is valid. A naming convention is a commonly followed style that makes code easier to read.
Python programmers often follow these conventions.
A. Use descriptive names for variables
Instead of:
x = 500
Prefer:
total_marks = 500
The second name clearly explains what the value represents.
B. Use lowercase letters for variable names
A common Python style is to write variable names in lowercase.
Example:
student_name = "Rahul"
student_age = 20
This is easier to read and follows the usual Python naming convention.
C. Use underscores to separate words
Python commonly uses snake_case for variables and functions.
Example:
first_name = "Rahul"
total_price = 1500
calculate_average = 90
Each word is separated by an underscore.
D. Use capitalized words for class names
Python commonly uses PascalCase for class names, where each word starts with a capital letter.
Example:
class StudentDetails:
pass
Here, StudentDetails is the class name.
You will learn more about classes when studying object-oriented programming.
E. Use uppercase names for constants
Programmers commonly use uppercase letters for names intended to represent constants.
Example:
MAX_MARKS = 100
PI = 3.14159
Python does not automatically prevent these names from being reassigned. Uppercase letters communicate the programmer's intention that the values should remain unchanged.
6. Identifiers in a Python Program
Let's look at a complete example.
student_name = "Rahul"
english_marks = 85
computer_marks = 95
total_marks = english_marks + computer_marks
print("Student Name:", student_name)
print("Total Marks:", total_marks)
Output:
Student Name: Rahul
Total Marks: 180
Let's identify the names used in the program:
student_nameidentifies the student's name variable.english_marksidentifies the English marks variable.computer_marksidentifies the computer marks variable.total_marksidentifies the variable that stores the sum.
Each identifier helps explain the purpose of the corresponding variable.
7. Can an Identifier Have Only an Underscore?
Yes. A single underscore is a valid identifier in Python.
For example:
_ = 100
print(_)
Output:
100
However, a single underscore is also used by Python programmers for certain conventions, such as indicating that a value is intentionally being ignored.
Example:
name, _ = ("Rahul", 20)
print(name)
Output:
Rahul
In this example, the second value is assigned to _ because the program does not need to use it.
8. Common Mistakes Beginners Should Avoid
Mistake 1: Starting a name with a number
Incorrect:
2marks = 80
Correct:
marks2 = 80
Mistake 2: Using spaces in names
Incorrect:
student age = 20
Correct:
student_age = 20
Mistake 3: Using a keyword as a variable name
Incorrect:
while = 5
Correct:
count = 5
Mistake 4: Using confusing names
Less descriptive:
a = 500
More descriptive:
total_marks = 500
Mistake 5: Mixing capitalization inconsistently
Consider:
student_name = "Rahul"
print(Student_Name)
This will raise a NameError because Student_Name and student_name are different identifiers.
Correct:
student_name = "Rahul"
print(student_name)
9. Practice Questions
Test your understanding with these questions.
Question 1: What is an identifier in Python?
Question 2: Write any five valid identifiers.
Question 3: Identify the invalid identifiers:
student_name2studenttotal_marksstudent ageclass
Question 4: Why can an identifier not start with a digit?
Question 5: Are age, Age, and AGE the same identifier?
Question 6: Rewrite the following identifiers correctly:
student name1numbertotal-marksif
Question 7: What naming style is commonly used for Python variables?
Question 8: Write a Python program that creates identifiers for a student's name, roll number, and marks, then displays their values.
10. Practice Program: Student Information
Try this simple program:
student_name = input("Enter student name: ")
roll_number = int(input("Enter roll number: "))
marks = float(input("Enter marks: "))
print("Student Name:", student_name)
print("Roll Number:", roll_number)
print("Marks:", marks)
Example output:
Enter student name: Rahul
Enter roll number: 12
Enter marks: 87.5
Student Name: Rahul
Roll Number: 12
Marks: 87.5
Notice how descriptive identifiers make the program easy to understand.
Final Thoughts
Identifiers are the names we use to identify variables, functions, classes, and other named objects in Python.
Remember these essential rules:
Start an identifier with a letter or underscore, not a digit.
Do not use spaces or ordinary special characters in identifiers.
Never use a reserved keyword as an ordinary variable name.
Remember that Python is case-sensitive.
Choose descriptive names and follow consistent naming conventions.
The best way to learn identifiers is to practise writing small Python programs and use meaningful names in every example.
Comments
Post a Comment