Python id() builtin function takes an object as argument and returns its id value. id is an integer which is constant and unique for an object during its lifetime.
In this tutorial, you will learn the syntax of id() function, and then its usage with the help of example programs.
The syntax of id() function is
|object||Any valid Python object.|
1. Get id of a string object
In the following program, we take a string object in variable x, get its id, and print it to standard output.
2. Get id of a list object
In the following program, we take a list object in variable x, get its id, and print it to standard output.
3. No two objects will have same id
In a given lifetime of an object, it has unique id.
In the following program, we take two objects in variables x and y, get their id values, and print them to standard output. They will be different no matter how many times you run the program, because, id() function guarantees their uniqueness.
Run Code Copy
x = [5, 7, 0, 4] y = 'hello world' x_id = id(x) y_id = id(y) print('x id :', x_id) print('y id :', y_id)
x id : 4379396352 y id : 4379410224
In this tutorial of Python Examples, we learned the syntax of id() builtin function, and how to read id of an object, with examples.