Python Variables don't VARY!

a = 10
print(a)
a = 11
print(a)
OUTPUT10
11
#print(id(a))  NameError: name 'a' is not definedprint(id(10))a=10
print(id(a))
OUTPUT94581960420128
94581960420128
  1. id() knew 10 as all the numbers are int objects
  2. = operator assigned the int object 10’s memory address in ‘a’
a = 10
print(a)
print(id(a))
a = 11
print(a)
print(id(a))
b = 10
print(b)
print(id(b))
OUTPUT10
94581960420128
11
94581960420160
10
94581960420128
  1. There are 2 int objects 10 & 11, which are pointed by b and a respectively.
  2. After a changed the reference from 10 to 11, 10 object remained in the same address so when b was assigned 10 that same old 10 was assigned to ensure code reusability.
for i in int,float,tuple,list,complex,str,print,id,type,True,False, None :print(id(i))OUTPUT94046028248960 
94046028234464
94046028277216
94046028244384
94046028811776
94046028307424
140465722602752
140465722601712
94046028291392
94046028194048
94046028194080
94046028262848
int = 456
float = -87
complex = 76.78
print(int, float, complex)
print(int(),float(), complex())
print=97
print(print)
OUTPUTTypeError: 'int' object is not callable

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store