{"id":24803,"date":"2021-11-02T09:47:57","date_gmt":"2021-11-02T04:17:57","guid":{"rendered":"https:\/\/python-programs.com\/?p=24803"},"modified":"2021-11-05T20:36:28","modified_gmt":"2021-11-05T15:06:28","slug":"python-id-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-id-method-with-examples\/","title":{"rendered":"Python id() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python hex() Method with Examples<\/a> The id() function generates a unique id for the specified object.<\/p>\n In Python, each object has its own unique id.<\/p>\n When an object is created, it is given an id.<\/p>\n The id is the memory address of the object, and it will be different each time you run the program. (Except for objects with a fixed unique id, such as integers ranging from -5 to 256)<\/p>\n Note:<\/strong> The integer has a unique id. Throughout the lifetime, the integer id remains constant.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n object:<\/strong> Any object, such as a string, number, list, or class.<\/p>\n Return Value:<\/strong><\/p>\n The id() function returns the object’s identity. This is a unique integer for the given object that remains constant throughout its lifetime.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Fed up with searching various pages for the list of Python Built in Functions<\/a>? Look at the tutorial linked here and explore all coding samples of built-in functions of python.<\/p>\n In the previous article, we have discussed Python hex() Method with Examples id() Method in Python: The id() function generates a unique id for the specified object. In Python, each object has its own unique id. When an object is created, it is given an id. The id is the memory address of the object, …<\/p>\n
\nid() Method in Python:<\/strong><\/p>\nid(object)<\/pre>\n
Given number = 9\r\nGiven string = \"btechgeeks\"\r\nGiven list = [\"hello\", 123, \"this\", \"is\", \"btechgeeks\"]<\/pre>\n
The id of the given number = 11094560\r\nThe id of the given string = 140697796571568\r\nThe id of the given list = 140697796536328<\/pre>\n
Given number = 10.5\r\nGiven string = \"good morning btechgeeks\"\r\nGiven list = [1, 3, 5, 2]<\/pre>\n
The id of the given number = 140002720203304\r\nThe id of the given string = 140002691658064\r\nThe id of the given list = 140002691627016<\/pre>\n
id() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numbr = 9\r\n# Give the list as static input and store it in another variable.\r\ngvn_lst = [\"hello\", 123, \"this\", \"is\", \"btechgeeks\"]\r\n# Give the string as static input and store it in another variable.\r\ngvn_str = \"btechgeeks\"\r\n# Pass the given number as an argument to the id() function that generates a\r\n# unique id for the given number.\r\n# Store it in another variable.\r\nnumbr_id = id(gvn_numbr)\r\n# Pass the given string as an argument to the id() function that generates a\r\n# unique id for the given string.\r\n# Store it in another variable.\r\nstr_id = id(gvn_str)\r\n# Pass the given list as an argument to the id() function that generates a\r\n# unique id for the given list.\r\n# Store it in another variable.\r\nlst_id = id(gvn_lst)\r\n# Print the id of the given number.\r\nprint(\"The id of the given number = \", numbr_id)\r\n# Print the id of the given string.\r\nprint(\"The id of the given string = \", str_id)\r\n# Print the id of the given list.\r\nprint(\"The id of the given list = \", lst_id)\r\n<\/pre>\n
The id of the given number = 11094560\r\nThe id of the given string = 139634246537648\r\nThe id of the given list = 139634246502408\r\n<\/pre>\n
Example of id() for a class:<\/h5>\n
class number:\r\n numb = 6\r\n\r\n\r\nnumb_id = number()\r\nprint('The id of given number =', id(numb_id))\r\n<\/pre>\n
The id of given number = 140125486982872<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the number as user input using the float(input()) function and store it in a variable.\r\ngvn_numbr = float(input(\"Enter some random number = \"))\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in another variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the string as user input using the input() function and store it in another variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Pass the given number as an argument to the id() function that generates a\r\n# unique id for the given number.\r\n# Store it in another variable.\r\nnumbr_id = id(gvn_numbr)\r\n# Pass the given string as an argument to the id() function that generates a\r\n# unique id for the given string.\r\n# Store it in another variable.\r\nstr_id = id(gvn_str)\r\n# Pass the given list as an argument to the id() function that generates a\r\n# unique id for the given list.\r\n# Store it in another variable.\r\nlst_id = id(gvn_lst)\r\n# Print the id of the given number.\r\nprint(\"The id of the given number = \", numbr_id)\r\n# Print the id of the given string.\r\nprint(\"The id of the given string = \", str_id)\r\n# Print the id of the given list.\r\nprint(\"The id of the given list = \", lst_id)\r\n<\/pre>\n
Enter some random number = 10.52\r\nEnter some random List Elements separated by spaces = 12 45 6 9 1\r\nEnter some random string = good morning btechgeeks\r\nThe id of the given number = 139861816847568\r\nThe id of the given string = 139861816051792\r\nThe id of the given list = 139861816050144<\/pre>\n
\n