Python : Find out the variable type and determine the type with simple test
Couple of ways to determine or find out the type of a variable or object in Python. Listing down the methods here for future reference :
Use type(variable) method
num_array = [0,1,2,3]
type(num_array)
> <class 'list'>
t = ()
type(t)
> <class 'tuple'>
Sometimes we are not sure what the variable or object type is before performing operation on the variable. We can test it by type(variable) is
. For example :
type(num_array) is list
>True
type(num_array) is str
>False
or with isinstance()
function :
isinstance(num_array, list)
>True
isinstance(num_array, str)
>False
By the way, you can do this way as well if you prefer. With __class__
example :
num_array.__class__
> <class 'list'>
See also : Python : Convert(cast) bytes to string example
By
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+25.9k Mac/Linux and Golang : Fix bind: address already in use error
+9.4k Golang : Extract or copy items from map based on value
+18.3k Golang : Example for RSA package functions
+7.2k Golang : How to convert strange string to JSON with json.MarshalIndent
+20k Golang : Count number of digits from given integer value
+7.7k Golang : Regular Expression find string example
+5.6k Linux/Unix/PHP : Restart PHP-FPM
+16.7k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+7.3k Golang : Hue, Saturation and Value(HSV) with OpenCV example
+19.1k Golang : Populate dropdown with html/template example
+8.9k Golang : Go as a script or running go with shebang/hashbang style
+8.4k Golang : Another camera capture GUI application with GTK and OpenCV