In this article we will learn what are identifiers in Python with example and know the rules for naming identifiers.
What are Identifiers in Python?
Python identifiers are name that is used by the programmer to identify some variable, data or functions.
Python identifiers are used to give the variable, constant, or function a name that can be used later to invoke or call that function or to identify some data.
Python identifiers can be letters (a-z, A-Z), words, digits (0-9), a combination of letters and digits, or a combination of words and underscore.
Python is a case-sensitive language therefore engg and Engg are treated as two different identities.
Example of Identifier in Python:
#Program to demonstrate Python Identifier Example: _a = 10 print(_a) a_b = 20 print(a_b)
In the above program there are two variables and the name of those variables “_a” and “a_b” is an identifier.
Python Identifier Examples
Is Python case sensitive when dealing with identifiers?
Yes, Python is a case sensitive Language when dealing with identifiers.
Capital and small letters are treated as two different identifiers.
Example: ‘A’ and ‘a’ are completely two different identifiers.
Rules for Naming Identifiers in Python
An Identifier in Python can be any name starting with either alphabet A to Z, a to z, or underscore ( _ ) followed by one or more letters, underscores, or digit.
An identifier can never start with any numeric digit.
‘1Enggtutorial’ is not valid identifier because it starts with digit.
‘Enggtutorial1’ is a valid identifier because it starts with an alphabet.
Punctuation symbols like @, #, $ etc. cannot be used in identifiers.
Frequently Asked Questions:
Difference between Python Keywords and Identifiers?
In Python, keywords are reserved words that are stored inside Python libraries and these keywords are used in Python programs to perform some specific task. For example break keyword in Python is used to terminate the loop inside a program.
Where as Identifiers are any name that are used to identify variables, functions or any constant.
The difference between Python keywords and identifiers is that the keywords cannot be used as an identifier because these are reserved word and cannot be used as variables or identifier.
and, or, true, false, await, break etc. these words are reserved words whose task to perform some task and they cannot be used as an identifier.
Difference between Python Variables and Identifiers?
A variable in Python refers to a memory location where the data of the variable can be stored and manipulated when needed.
The difference between variables and identifiers in Python is that variable has reserved memory but identifiers don’t have any memory they are just use to name any constant value, function or class so that they can be called with the same name in the entire program whenever required.