Identifier refers to name given to entities such as variables, functions, structures etc. It means Identifiers is used to identify functions, variables, module, class or any object.
The identifier must be unique. They are created to give a unique name to an entity to identify it during the execution of the program. For example:
name = "Rushikesh"
a = 10
b = 20
c = 30
In above example
c are identifiers. Here, we can identify the
name contain the string literal and
c contain integer values.
An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).
Rules for writing identifiers
- An identifier must be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_).
- We cannot use special symbols like !, @, #, $, % etc. in our identifier.
- Python is a case sensitive programming language. Thus, BeginnersBook and beginnersbook are two different identifiers in Python.
- An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.
- Keywords (see section below) cannot be used as identifiers.
- Starting an identifier with a single leading underscore indicates that the identifier is private
- Starting an identifier with two leading underscores indicates a strongly private identifier.
- If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.
Things to care about
Python is a case-sensitive language. This means
variable are not the same. Always name identifiers that make sense.
c = 10 is valid. Writing
count = 10 would make more sense and it would be easier to figure out what it does even when you look at your code after a long gap.
Multiple words can be separated using an underscore,
We can also use camel-case style of writing, i.e., capitalize every first letter of the word except the initial word without any spaces. For example: camelCaseExample