0

I am very new to Python.Is there any method through which i can find out which type of value is stored in a particular variable?

In below code how to find out type of stored value in a variable?

counter = 100          
miles   = 1000.0       
name    = "John"     
name    =10


print counter
print miles
print name
Kunal
  • 29
  • 1
  • 9

2 Answers2

1
>>> type('1')
<class 'str'>
>>> type(1)
<class 'int'>
>>> type(1.000)
<class 'float'>
>>> type({1:2})
<class 'dict'>
>>> type([1,2,3])
<class 'list'>
Abhijeetk431
  • 847
  • 1
  • 8
  • 18
1

Quite simply, use type command built into python.

For example, you could check the type of this variable:

>>> name = 'John'
>>> print(type(name))
<class 'str'>

Use this method wherever you want on a variable, and you don't necessarily have to print it. You could use it to do different things with different variables based on what type it is, for example.

Hoped this helped :)

Dylan Zipsin
  • 35
  • 1
  • 7