Javatpoint Logo
Javatpoint Logo

What is Identifier in Python?

In Python, an identifier is the name assigned to a variable, function, class, or other object. It is used to refer to the object in the program and is a way to give the object a human-readable name. In Python, an identifier must follow certain rules to be valid.

An identifier in Python must, by default, start with a letter or an underscore ( ). The identifier cannot begin with a number or any other special character. The remaining characters in the identifier can be letters, numbers, or underscores. This means that an identifier can contain letters, numbers, and underscores but cannot contain any other special characters.

The second rule for an identifier in Python is that it must be unique within the scope in which it is used. This means you cannot have two variables with the same name in the same scope or two functions with the same name in the same module. Attempting to do so will result in a Name Error.

The third rule for an identifier in Python is that it cannot be a reserved word. Python has a set of reserved words that cannot be used as identifiers. These include keywords like if, else, for, while, and so on. A Syntax Error will be returned if you attempt to use a reserved term as an identifier.

In Python, there is also a concept of naming conventions for identifiers, followed by python developers to make the code more readable and maintainable.

One of the most popular naming conventions is "CamelCase," which is used for variables and functions. First letters of succeeding words are uppercase in CamelCase, but the first letter of the identifier is lowercase. For example, "myVariable" and "myFunction" are valid CamelCase identifiers.

Another popular naming convention is "snake_case," which is used for variables and functions. Underscoring separates words in snake case, which uses lowercase letters for all identifier letters. For example, "my_variable" and "my_function" are valid snake_case identifiers.

Additionally, the built-in Python function id() retrieves an object's distinctive identification. The id() function takes one argument, which is the object for which you want to get the identifier. An original integer that is connected to the item serves as the returning identifier. This identifier will always be distinct and constant for the duration of the object.

Identifiers are an important part of Python programming, and they are used to give a human-readable name to a variable, function, class, or any other object in Python. An identifier must follow certain rules, such as starting with a letter or underscore, being unique within the scope, and not being a reserved word. Additionally, Python developers follow naming conventions like CamelCase and snake_case to make the code more readable and maintainable. The built-in id() function can be used to get the unique identifier of an object.

Here is an example of a program that demonstrates the use of identifiers in Python:

When you run the program, it will output the following:



In this program, we have a variable called myVariable, a function called my_function, and a class called MyClass. The variable and function use different naming conventions (CamelCase and snake_case, respectively) commonly used in Python. We also create an object of the MyClass class and assign it to the variable my_object.

At the end of the program, we use the id() function to get the unique identifier of each of these objects. The id() function takes an object as an argument and returns its unique identifier as an integer.

When you run this program, you will see the unique identifiers of the variable, function, and object printed on the screen. These identifiers are unique and constant for the lifetime of the objects and can be used to reference them in the program.

It is important to note that you should avoid using any Python-reserved words as an identifier in your program, or it will raise a SyntaxError.

Next TopicWhat is Rest API

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA