id() function in Python
Introduction
id() is an inbuilt function in Python.
Syntax:
id(object)
As we can see the function accepts a single parameter and is used to return the identity of an object. This identity has to be unique and constant for this object during the lifetime. Two objects with non-overlapping lifetimes may have the same id() value. If we relate this to C, then they are actually the memory address, here in Python it is the unique id. This function is generally used internally in Python.
Examples:
The output is the identity of the
object passed. This is random but
when running in the same program,
it generates unique and same identity.
Input : id(1025)
Output : 140365829447504
Output varies with different runs
Input : id("geek")
Output : 139793848214784
# This program shows various identities str1 = "geek"print(id(str1)) str2 = "geek"print(id(str2)) # This will return True print(id(str1) == id(str2)) # Use in Lists list1 = ["aakash", "priya", "abdul"] print(id(list1[0])) print(id(list1[2])) # This returns false print(id(list1[0])==id(list1[2])) |
Output:
140252505691448 140252505691448 True 140252505691840 140252505739928 False
Recommended Posts:
- Python map() function
- Python | dir() function
- Python | How to get function name ?
- Python | oct() function
- sum() function in Python
- Python | hex() function
- Python | cmp() function
- Help function in Python
- Python | int() function
- ord() function in Python
- Python | now() function
- Python | tuple() Function
- Python | fsum() function
- join() function in Python
- Python | globals() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



