id() function in Python


In this article, we will learn about the usage and implementation of id() function in Python 3.x. Or earlier. It is present in Python Standard Library and is automatically imported before executing the code.

Syntax: id (<entity name>)

Return value: Identity value of type <int>

The function accepts exactly one argument i.e. the name of the entity whose id has to be used. This id is unique for every entity until they are referring to the same data.

Id’s are merely address in the memory locations and is used internally in Python.

Example Code

str_1 = "Tutorials"
print(id(str_1))
str_2 = "Tutorials"
print(id(str_2))
# This will return True as string values are identical
print(id(str_1) == id(str_2))

# This will return False as string values are not identical
str_1=str_1+str_2
print(id(str_1) == id(str_2))

# This will return True as string references are identical
str_2=str_1
print(id(str_1) == id(str_2))

Output

46939355256048
46939355256048
True
False
True

Here in case, 1 bool value True is displayed as both string variables contains the same type of data. Whereas in case 2 the contents of one of the variables are modified by concatenation operations and hence bool value False is displayed on the screen. In case 3 references to both the string variables are identical and hence True is displayed on the screen.

Conclusion

In this article, we learned how to implement the lambda and filter() functions in Python 3.x. Or earlier. We also learned about the combined usage of both functions to get the desired output.

Updated on: 07-Aug-2019

170 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements