A name in Python is consider to be an identifier, it can be a class name, function name, module name or a variable name. In Python we have set of rules to define these identifiers (names).

Rules to define Python Identifiers :

In this tutorial, we will see what are the rules to define identifiers in python.

1. The only allowed characters in identifiers:

  • alphabet (lower and upper case)
  • digits (0-9)
  • underscore symbols (_)
salary = 1000   // valid
$alary = 1000   // invalid syntax

2. Identifiers should not start with digit :

  • 5Ruppes is not allowd
  • ruppes5 is allowwd

3. Python Identifiers are case sensitive :

  • Python language is case sensitive language
marks = 56
MARKS = 72

print(marks)  == >  56
print(MARKS)  == >  72

4. Should not use Reserved words :

As per Python document, we have 33 reserved words in Python. We should not allowed to use these reserved words as identifiers.

Example : def is a reserved word in Python, we should not use def as identifier.

def = 10    // invalid
if = 20    // invalid

5. $ symbol is not allowed :

In any cases $ symbol is not allowed as part of the identifiers.

ca$h = 200   // invalid identifier

List if all valid and invalid identifiers :

15total   // invalid
total15   // valid
python2torials  // valid
ca$h   // invalid
_online_tutorials_point   // valid
def   // invalid
if    // invalid

Points to note while defining identifiers :

  1. If an identifier starts with _ (underscore) symbol, then it indicates that it is private identifier.
  2. If an identifier starts with __ (two underscores) symbols, then it indicates that strongly private identifier.
  3. If an identifier starts and ends with __  (two underscores) symbols, then the identifier is language defined special name, it is also known as magic methods Example : __add__

Happy Learning 🙂

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment