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
+20k Golang : Determine if directory is empty with os.File.Readdir() function
+11.5k Golang : Display a text file line by line with line number example
+14.3k Golang : How to filter a map's elements for faster lookup
+9.6k Golang : Find correlation coefficient example
+12.5k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+5.7k Unix/Linux : Get reboot history or check when was the last reboot date
+11.6k Golang : Calculations using complex numbers example
+13.3k Golang : Read from buffered reader until specific number of bytes
+9.8k Golang : Check if user agent is a robot or crawler example
+5.3k How to check with curl if my website or the asset is gzipped ?
+19.7k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+14.2k Golang : Get uploaded file name or access uploaded files