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
+7.9k Golang : Take screen shot of browser with JQuery example
+6k Golang : Mapping Iban to Dunging alphabets
+7.3k Android Studio : Import third-party library or package into Gradle Scripts
+19.8k Golang : For loop continue,break and range
+4.3k Gogland : Datasource explorer
+12.4k Golang : Convert spaces to tabs and back to spaces example
+37.9k Golang : How to iterate over a []string(array)
+12.6k Golang : How to check if a file is hidden?
+28.1k Golang : Get time.Duration in year, month, week or day
+6.3k Golang : Getting Echo framework StartAutoTLS to work
+14.1k Golang : Get digits from integer before and after given position example
+10.1k Golang : Find age or leap age from date of birth example