Skip to content

Commit eff6ee3

Browse files
authored
[Term Entry] Python Built-in Function: .id() (#2486)
* Created id term for Python built-in function * Apply suggestions from code review I have added the wonderful suggestions! Thank you so much! The only thing I have to do now is to delete the second codebyte example as it was suggested that it's not needed. I will do that in VS Code and push the changes here! 😊 Co-authored-by: SSwiniarski <86081858+SSwiniarski@users.noreply.github.com> Co-authored-by: Daksha Deep <dakshadeep1234@gmail.com> * Update id.md I deleted the second codebyte example * Apply suggestions from code review I made additional suggested changes eliminating extra white space! 😊 Co-authored-by: Daksha Deep <dakshadeep1234@gmail.com> * Update content/python/concepts/built-in-functions/id/id.md Co-authored-by: Daksha Deep <dakshadeep1234@gmail.com> * Update and rename content/python/concepts/built-in-functions/id/id.md to content/python/concepts/built-in-functions/terms/id/id.md * review edits ---------
1 parent 8931b7c commit eff6ee3

File tree

1 file changed

+85
-0
lines changed
  • content/python/concepts/built-in-functions/terms/id

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
Title: 'id()'
3+
Description: 'Gives a unique number for any object in Python.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Functions'
9+
- 'Methods'
10+
- 'Id'
11+
- 'Memory'
12+
- 'Debugging'
13+
CatalogContent:
14+
- 'learn-python-3'
15+
- 'paths/computer-science'
16+
- 'paths/data-science'
17+
---
18+
19+
The **`id()`** function gives a unique number for any object in Python. This number is the location of the object in the computer’s memory. This will be consistent for the duration of the object's lifetime.
20+
21+
## Syntax
22+
23+
```pseudo
24+
id(object)
25+
```
26+
27+
The parameter, `object`, can be any given object such as a string, list, number, dictionary, etc.
28+
29+
## Example
30+
31+
In the example below, when two **immutable** variables are compared using the `id()` function, both return the same value pointing to the same location in memory.
32+
33+
This is because immutable objects don't change. The following example uses an immutable `string` object to demonstrate this:
34+
35+
```py
36+
color = 'green'
37+
38+
favColor = 'green'
39+
40+
print(id(color))
41+
42+
print(id(favColor))
43+
```
44+
45+
This example results in something resembling the following output:
46+
47+
```shell
48+
140307997340656
49+
140307997340656
50+
```
51+
52+
## Example 2
53+
54+
In this next example, the `id()` function will be executed with two **mutable** variables. Take note of how the function will return different values: two separate unique ids.
55+
56+
This is because mutable objects are able to change. A mutable `list` object can be used to demonstrate this:
57+
58+
```py
59+
animals = ['lions', 'tigers', 'bears']
60+
61+
favAnimals = ['lions', 'tigers', 'bears']
62+
63+
print(id(animals))
64+
65+
print(id(favAnimals))
66+
```
67+
68+
This example results in something resembling the following output:
69+
70+
```shell
71+
140279020355392
72+
140279020204352
73+
```
74+
75+
## Codebyte Example
76+
77+
The following example displays the output of two immutable `number` objects:
78+
79+
```codebyte/python
80+
hello = 30
81+
goodbye = 30
82+
83+
print(id(hello))
84+
print(id(goodbye))
85+
```

0 commit comments

Comments
 (0)