A Detailed Guide on Python Identifiers

Programming like any game has its own set of rules and guidelines within which the participants should play. Anything outside shall be disqualified with an error. Thereby, it is imperative that one understands what identifiers in Python are.

This article sets out to explore in detail the identifiers in Python so as to make clear the rules of the game before you could get your hands in.

  • What is an Identifier in Python?
  • Defining an Identifier
  • Validating an Identifier

What is an Identifier in Python?

Identifiers are the terms which in Python refer to the programming constructs as in modules or functions or classes or variables or any objects of such sorts. The coders define these at their own discretion. Nevertheless, identifiers are not to be confused with keywords which are exclusive words reserved in Python programming. Now that we know what an identifier is, let us have a look at how to define an identifier.


Defining an Identifier in Python

There are certain do’s and don’ts that ought to be followed when it comes to defining an identifier in Python. First off, here is a list of the things that are not to be done when you coin an identifier and deploying numbers is important among them. No identifier can start with a number or only contain numbers. For instance, Python does not accept 0xy or 123 as identifiers.

Syntax Errors When Starting With Numbers
Syntax errors when starting with numbers

The other noteworthy thing would be the usage of special characters while defining identifiers. The only special character that Python allows to be in an identifier is the underscore ( _ ). For instance, a+b is not valid as an identifier by Python standards, but a_b is! That being said, refrain from the usage of underscore to start or end an identifier since it shall interfere with the in-built features of Python.

Usage Of Special Characters In Identifiers
Usage of special characters in Identifiers

It is also to be noted that Python is case-sensitive when it comes to naming conventions. This language considers AB, Ab, aB and ab as different entities independent from each other. So, one needs to be a bit cautious while juggling around while using different cases in coining the identifiers.

Usage Of Identifiers With Different Cases
Usage of Identifiers with different cases

Python follows a ‘no-holds barred’ approach when it comes to the usage of characters in the identifiers however it is not a normal thing for a coder to pick a multi-syllable term straight out of a dictionary from the Victorian era and call it an identifier. Make it short and simple!

When declaring class names, it is recommended to start your identifiers with an uppercase letter to make it stand out from the crowd such as Address, Price, Output instead of address, price and output. But the same approach is to be flipped in case of declaring functions or variables where the lowercase letters are to be used such as finder( ), data1 and so.


Validating an Identifier in Python

Having read all the best practices and those which should not be done while defining an identifier, one might begin to wonder, ‘Is there a way around which tells us whether the chosen identifier is something that is valid by the rules of Python or not?’. We hear you and here is how that can be verified – using a combination of isidentifier( ) and iskeyword( ) functions. The latter validates whether the chosen belongs to the list of keywords in Python whilst the former verifies whether the chosen satisfies the other conditions to be an identifier.

import keyword
def identifier_verifier(x):
    if not keyword.iskeyword(x):
        return x.isidentifier()
    else:
        print("Invalid identifier")
print(identifier_verifier("and"))
print(identifier_verifier("ab3"))
Validating An Identifier
Validating an identifier

Conclusion

Now that we have reached the end of this article, hope it has given a detailed elaboration on the Identifiers and their corresponding usage in Python. Here’s another article that can help you understand the workings of a for loop in Python. There are numerous other enjoyable and equally informative articles in CodeforGeek that might be of great help for you to gain valuable insights. Until then, Carpe diem!


Reference

https://docs.python.org/3/reference/lexical_analysis.html#identifiers

Arulius Savio
Arulius Savio
Articles: 26