Rules to define Python Identifiers

By | 2018-03-27T20:13:51+05:30 March 27th, 2018|Python|0 Comments

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 symbols (either lower case or 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:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment