How can we know all possible type of a variable? Consider the following example,
n = int(raw_input())
if n<50:
a = 5
elif n<100:
a = 6.8
else:
a = 'abc'
print type(a)
But, It will give output as either 'int' or 'float' or 'str', depending on value of n...
Can't we have all possible types of a variable? For example the above should give an output something like :
{int,float,str}
No, because variables in Python don't have type; values do.
When you write
type(a)
, you're not checking the type of a variable nameda
; you're checking the type of a thing that the namea
refers to (which might happen to have other names as well).