id() Function in Python

Video Tutorial
FREE
Id function thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate
Topics Covered

Overview

The id is a function that will not require separate installations, and it comes with every default Python installation.

In programming, an id is a uniquely identifiable number assigned to objects and variables created during the program. They are similar to the unique identification cards that most of us use in our day-to-day lives.

Syntax of id() Function in Python

Let's see how the id function in Python works:

Output :

Here, a variable var was created and the value 12 was stored. The value returned by id() for the variable var is its unique id. Only the variable var can hold that id.

The id() in Python helps to treat variables and objects created in it uniquely.

Parameters of id() Function in Python

The id() function only takes a single parameter, which is the parameter object.

The parameter object can be treated as almost anything used in Python.  The object parameter can range from constants and variables to a custom class object created in Python.

Return Values for id() Function in Python

The value returned by the id() function is a unique id held only by the specified object.

The id() function will always return an integer value as the unique id.

Output :

Here, a variable var was created and a value of 19 was stored. We can see that the id() function returned the variable var's unique id and we can see that the type() returned a class int object, which implies that the value returned by the id() function is an integer.

Exceptions for id() Function in Python

As mentioned above, the id() function in Python returns the unique id for the object. But there is an exception.

Let's see what that exception is:

Output :

Here, we can see that two variables, var_1 and var_2 respectively, were created and the value 23 was stored in both of them.

We can observe here that, the id() function returned the same unique id for both var_1 and var_2.

So Why did this happen ?
This is because the unique id was created for the value 23, and var_1 and var_2 are just two references to that value. You can try more examples with variables pointing to the same value, but the id() function in Python will always return the same unique id for all those variables.

This is considered one of the many ways a Python interpreter efficiently manages its memory.

Examples of id() Function in Python

Let's see id() function in action :

Output :

As you can see, three variables were created, var_1, var_2, and var_3 respectively.

Note that var_1 points to the value 11, and var_2 and var_3 point to the value 19.

We can clearly see that, the id() function returned a different id for var_1 and the same id for var_2 and var_3. This is because var_1 points to a different value than var_2 and var_3 and unique ids are created for the values created in the memory. Hence, var_2 and var_3 will have the same id because they are pointing to the same object in the memory.

What is id() in Python?

Now let's go a little deep into the id() function.

Let's understand further by asking some questions.

What is an id?

An id, in programming, is a uniquely identifiable number assigned to objects and variables created in the program.

An id stands for the identity of an object. It is used to uniquely identify an object or a variable created during the lifetime of a program.

What is the id() Function in Python?

id() is an inbuilt Python function that returns a unique id for the given object, and this id has to be unique during the lifetime of the object.

All objects and variables in Python have their unique id. You can think of this as similar to the process ids  used by operating systems to uniquely identify each process.

Every object created in Python when stored in the memory is assigned a unique identification number that helps the Python interpreter to perform better and utilize memory efficiently.

The id() function returns this unique identity held by the objects.

The id() function in Python requires no additional installation, it is included with the default Python installation in any environment (e.g., Anaconda, Python IDLE, etc.).

No separate import statements are required to call this function.

The ID is assigned to every object when it is created in memory.

The ID is the object's memory address and will be different each time you run the program. except for some objects that have a constant unique id, like integers from -5 to 256.

Some points to be noted while using the "id()" function are that :

  • The unique integer id returned by the id() function will be the same for variables pointing to the same value.
  • During assignment operations on variables, the id will not change for the new variable, it will remain the same as the previous id.

More Examples

Let's dive into some more examples to understand more about the id() function in Python.

Let's look through the values returned by the id() function for different data types.

int :

Output :

Here the id() function returned an integer value as the unique id for the integer variable var.

float :

Output :

Here the id() function returned an integer value as the unique id for the float variable var.

str :

Output :

Here the id() function returned an integer value as the unique id for the string var.

Lists :

Output :

Here the id() function still returned an integer value as the unique id for the list items.

Tuples :

Output :

Here the id() function still returned an integer value as the unique id for the tuple items.

Class Objects :

Output :

Here, a class student was created, which had string variables called name and division and integer variables called age and adm_no (Admission Number).

An object of the student class,std_1 was created.

We can still observe that the id() function still returned an integer value as the unique id for the object std_1.

Now let's look at different cases.

Assignment Operation :

Let's see if the assignment operation changes the id.

Output :

We can see that the id did not change. This is because both variables var_1 and var_2 are pointing to the same object in the memory.

The principle applies to lists and arrays as well.

Here, a point to be noted is that, since both variables are pointing to the same object, a change in any one of the variables, will lead to a change in the other variable.

Updating Values :

Let's see if updating a value in the variable will cause a change in the id of the object.

Output :

From this, we can infer that an update in the variable creates a new id. This is because a new object with the updated value was created in the memory and the variable var is referenced to the new object.

Varying Run Times :

Let's see what the id() function will return for the same object at different run times.

Output :

Let's use this again,

Output :

As we can see, different ids are produced during different run times.

Conclusion

  • The id function in Python does not require any separate imports or installations.
  • id function in Python returns the unique identity of the specified object.
  • The id is assigned when a memory object is created.
  • The id function always returns an integer value as the unique id of the object.
  • Variables pointing to the same memory object will have the same id.
  • The assignment operation on variables will not cause a change in the id.

See Also

Since you now know about the id() function in Python, why not look into some other similar Python functions in the official Python documentation?