Q.Example 5.2:
num1 = 20
id(num1)
1433920576 #identity of num1
num2 = 30 - 10
id(num2)
1433920576 #identity of num2 and num1
#are same as both refers to
#object 20
num1 = 20
id(num1)
1433920576 #identity of num1
num2 = 30 - 10
id(num2)
1433920576 #identity of num2 and num1
#are same as both refers to
#object 20
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →id() reveals which object a variable refers to — num1 = 20 and num2 = 30 − 10 both point at the same int object 20, so their ids are identical (the exact number varies by machine/session).
The concept is that a Python variable is a reference to an object, not a box holding a value. id(obj) returns an integer that uniquely identifies that object during its lifetime (in CPython it is the object's memory address). Two variables can refer to one object — and then their ids match.
>>> num1 = 20
>>> id(num1)
1433920576 # identity of num1
>>> num2 = 30 - 10
>>> id(num2)
1433920576 # same identity: num2 refers to the same object 20
Step by step:
| Step | What happens |
|---|---|
num1 = 20 | num1 is bound to the integer object 20 |
id(num1) | prints that object's identity |
num2 = 30 - 10 | the expression evaluates to 20; num2 is bound to the same cached object 20 |
id(num2) | prints the identical identity |
Why the same object? CPython pre-creates and reuses (interns) small integers — roughly the range −5 to 256 — so every occurrence of the value 20 anywhere in the program refers to one shared object. That is why the ids coincide here. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.