Tuesday, July 30, 2013

How to identify Python variable data types?

Here is a quick way to tell what type of variable you are working with in Python by using the type() function:

jemurray@dsg:~ $ python
Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information. 
>>> a = {}
>>> type(a)
>>> a = []
>>> type(a)
>>> a = ()
>>> type(a)
>>> a = ''
>>> type(a)

In many Python examples, the output is displayed something like this:

>>> print a
(1, 2, 3, 4)
If you didn't know what the () around the output meant, you can use the above example to figure it out.  


No comments:

Post a Comment

Followers