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
+6.4k Golang : Lock executable to a specific machine with unique hash of the machine
+10.5k Golang : Sort and reverse sort a slice of runes
+12.5k Golang : Human readable time elapsed format such as 5 days ago
+4.6k Linux/Unix/PHP : Restart PHP-FPM
+9.2k Golang : How to profile or log time spend on execution?
+9.5k Golang : Get UDP client IP address and differentiate clients by port number
+3.5k Golang : Valued expressions and functions example
+8.5k Golang : Eroding and dilating image with OpenCV example
+16.1k Golang : Multi threading or run two processes or more example
+6.6k Golang : Generate human readable password
+7.6k Golang : Convert(cast) []byte to io.Reader type
+22.3k Golang : Check if element exist in map