Updated April 14, 2023
Introduction to Python Identifiers
The following article provides an outline on Python Identifiers. Python identifier is the name associated to an entity in python. In other words, it can be termed as the naming convention used in python programming. The entities considered for applying the naming conventions are namely classes, variables, items, function etc. The major advantage of using these identifier considerations on the naming entities helps to differentiate or identify one entity from the other. Also, there is the capability to test the validity of a given specific identifier in python programming.
Rules for Python Identifiers
Given below are the rules:
Rule 1
The identifiers in python programming can be framed as a combination of both uppercase ( A to Z), lowercase (a to z) and digit values( 1 to 9). In addition to the uppercase, lowercase and digit values, symbols like underscores can also be used. Some among the examples of these identifier values are listed below:
Example:
num1, sample_class, Test_variable.
Rule 2
A digit value can never be used for representing the start of the identifier value. Any instance where a digit is prefixed to the variable name is considered to be an invalidly declared python identifier.
Example:
1num , 7Test_class , 3Employee_function.
Code:
7_Bikes={'Bike_1':'Bullet','Bike_2':'Apache','Bike_3':'Fazer'}
7_Bikes.update({'Bike#_4': 'Discover'})
print("Top Bikes list after update:", 7_Bikes)
Output:
Explanation:
- We can notice the dictionary type element is prefixed with an identifier name that is of a digit.
- Passing this digit prefixed value to an interpreter throws an error stating that an invalid token has been associated with that specific identifier.
Rule 3
Non among the predefined keywords can be used as an identifier in python. The predefined keywords are reserved words in python. Keywords being the reserved words for a python programming language, it means like these words cannot be used in any other user instances instead of their proprietary use. There are in and around 33 keywords in python.
The overall list of keywords in python are listed below:
Python Reserved Keywords | ||||
FALSE | await | Else | import | pass |
None | break | except | in | raise |
TRUE | class | finally | is | return |
and | continue | For | lambda | try |
as | def | From | nonlocal | while |
assert | del | global | not | with |
async | elif | If | or | yield |
Example:
Code:
def={'Bike_1':'Bullet','Bike_2':'Apache','Bike_3':'Fazer'}
7_Bikes.update({'Bike#_4': 'Discover'})
print("Top Bikes list after update:", def)
Output:
Explanation:
- We can notice the reserved keyword def is used as the identifier for declaring the python dict variable.
- So, as a result of this, at the time of interpretation of the code, it throws an error stating an invalid token has been allocated.
- This depicts how the python interpreter throws a syntax level error when a reserved keyword is invalidly used as an identifier for a variable.
Rule 4
Identifiers cannot hold any kind of special characters other than _ in its execution. Some among the special characters which are not acceptable for a identifier declaration are as listed ” !, @, #, $, %, ^, &, *, ( , ), ~, “.
Example #1
Code:
dic_v@ar={'Bike_1':'Bullet','Bike_2':'Apache','Bike_3':'Fazer'}
[email protected]({'Bike#_4': 'Discover'})
print("Top Bikes list after update:", dic_v@ar)
Output:
Explanation:
- Here the special character ‘@’ is used in the declaration of the dict variable, but we can notice the use of the ‘@’ special character in the variable restricts the variable from binding a valid reference and throws the error stating can’t assign to an operator.
Example #2
Code:
dic_v%ar={'Bike_1':'Bullet','Bike_2':'Apache','Bike_3':'Fazer'}
dic_v%ar.update({'Bike#_4': 'Discover'})
print("Top Bikes list after update:", dic_v%ar)
Output:
Explanation:
- Here the special character ‘%’ is used in the declaration of the dict variable, but we can notice the use of the ‘%’ special character in the variable restricts the variable from binding a valid reference and throws the error stating can’t assign to an operator.
Example #3
Code:
dic_v&ar={'Bike_1':'Bullet','Bike_2':'Apache','Bike_3':'Fazer'}
dic_v&ar.update({'Bike#_4': 'Discover'})
print("Top Bikes list after update:", dic_v&ar)
Output:
Explanation:
- Here the special character ‘&’ is used in the declaration of the dict variable, but we can notice the use of the ‘&’ special character in the variable restricts the variable from binding a valid reference and throws the error stating can’t assign to an operator.
Rule 5
The length of the identifier is flexibly open.
Which means the identifier can be of any specific length.
Best Practices on Python Identifiers
- An upper case letter is always used to denote a beginning of the class, whereas a no upper case is preferred for all other identifiers that have been declared.
- When an entity needs to be denoted as a private entity, like the contents of the entity are not preferable to use for any other instance, that entity can be denoted with a _ (underscore) in the beginning.
- While declaring indexes for Iterators, it is always preferable to use a word instead of mentioning just a single character for the iterator index. Example: Iterator name of ‘index’ is better than using the iterator value of ‘i’.
- Python is a case sensitive language, so identifiers with case sensitivity are completely different ones. Example: Identifier name ‘Number_variable’ is very different from identifier name ‘number_variable’. Both are considered to be different variables in python programming.
Example with Precise Identifiers
Given below is the example:
Code:
class Person_class:
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
def name_method(self):
print(self.first_name, self.last_name)
x = Person_class("Mike", "Gordon")
x.name_method()
Output:
Explanation:
- The above program includes a valid python class and a python function which are responsible for taking in string values and post the corresponding combined string to the console.
- The class name and object values represented here are very much adhered to protocols of identifiers and accordingly declared.
- We can notice the clubbed string being nicely displayed in the console.
Conclusion
The process of associating identifiers is a primary content for any programming language, and from a python perspective, this has been very flexibly implemented in python. This concept is very similar to variable declarations here from python perception, the complexity of this is reduced by introducing many python-oriented protocols. The concepts like identifiers offer extensive stability in coding standards of python.
Recommended Articles
We hope that this EDUCBA information on “Python Identifiers” was beneficial to you. You can view EDUCBA’s recommended articles for more information.