In this tutorial, you will all about the Python id() function to find the unique id for the specified objects. id function in Python is the best built-in function to get the id of a specified object( string, list, number, set, etc). In the previous tutorial, we have seen the Python hex function to convert numbers to hexadecimal.
Headings of Contents
Python id() function Introduction
id() function is a built-in function that is used to find the unique id of any specified object. All the objects have their own unique id. id is assigned to an object when the object is created. The id of the object will be different each time you run the program.
Syntax
The syntax of id() function is:-
id(object)
Parameter
The Python id() function takes one parameter, that is Python object like a string, list, tuple, set, dictionary, etc.
Return Value
The return value of the id function in Python is the unique id of the object.
Python id() Example
In this example section, I created multiple variables and after that tried to get unique id of all that variables using the id() function.
Example: getting the id of variables
#id of the number num = 120 print(id(num)) #id of the string mystring = 'Programming Funda' print(id(mystring)) #id of the set my_set = {1, 2, 3, 4} print(id(my_set)) #id of the tuple my_tuple = ('Python', 'Java', 'JavaScript') print(id(my_tuple)) #id of the list my_list = [1, 2, 3, 4, 5, 6] print(id(my_list)) employee = { "employee_name": "Vishvajit", "employee_age": 25, "employee_salary": 23000, "employee_designation": "Dev" } # getting the id of that dictionary employee print(id(employee))
Output
140706168055168
2254935321136
2254936994528
2254936646464
2254936963328
1521805605568
Conclusion
In this tutorial, you have learned the id() function to find the id of the object along with an example. id() function comes under Python built-in functions which means you don’t need to download it by using the pip command. If this article helps you, please share and keep visiting for further Python built-in functions tutorials which are listed below.
Python built-in functions
For more information:- Click Here