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
+29.5k Golang : JQuery AJAX post data to server and send data back to client example
+15.4k nginx: [emerg] unknown directive "ssl"
+11.3k Google Maps URL parameters configuration
+48k Golang : Convert int to byte array([]byte)
+22.8k Golang : Strings to lowercase and uppercase example
+9.5k Golang : Find the length of big.Int variable example
+14.5k Golang : Simple word wrap or line breaking example
+14.9k Golang : Normalize unicode strings for comparison purpose
+18.1k Golang : Get all upper case or lower case characters from string example
+7.4k Golang : Calculate how many weeks left to go in a given year
+8.2k Golang : Get all countries phone codes
+5.4k Golang : Intercept, inject and replay HTTP traffics from web server