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
+11.3k Golang : How to use if, eq and print properly in html template
+7.6k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+13.6k Golang : Qt progress dialog example
+9.3k Golang : How to get username from email address
+9.4k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+11.7k Golang : How to detect a server/machine network interface capabilities?
+28.6k Get file path of temporary file in Go
+6.2k Golang : Extract sub-strings
+13.7k Golang : Tutorial on loading GOB and PEM files
+6.3k Golang : How to search a list of records or data structures
+9.4k Golang : Create unique title slugs example
+15.3k Golang : Get all local users and print out their home directory, description and group id