You are currently viewing How to Determine a Python Variable Type?

The Python built-in type() function provides a quick and easy way to determine or find a variable type. In this article, we will explore various techniques to determine a variable’s type, including the isinstance() function and specific methods to determine the type. For example, to check if a variable is a float or an integer.

Advertisements

1. Quick Examples to Determine a Python Variable Type

These are quick examples of how to determine a variable’s type using the type() and isinstance() functions. We will discuss these examples in more detail.


# Quick Examples to determine a python variable's type

# type() function to determine a variable's type 
# Type of int
age = 23
print(type(age)) # class 'int'

# Type of string
greeting = "SparkByExamples"
print(type(greeting)) # class 'str'

# Type of list
numbers = [1, 2, 3]
print(type(numbers)) # class 'list'

# Type of dictionary
my_dict = {"user": "ali", "age": 25}
print(type(my_dict))  # class 'dict'

# isinstance() function to determine 
# if a variable is of a certain type
print(isinstance(age, int)) 
# True

print(isinstance(greeting, str))
# Output 
# True

print(isinstance(numbers, list)) 
# True

print(isinstance(my_dict, dict)) 
# True

2. Use type() to get Python Variable Type

The Python type() is a built-in function that finds the data type of a variable. It takes a variable as an argument and returns the type of the variable. This function is useful when you want to check the data type of a variable or perform certain operations based on the data type. The type() function returns the type of the object in the format <class 'type'>.

See the following example we have used with the type() function to check the variables’ type:


# The type() function with a Boolean variable
x = True
print(type(x)) 
# Output:
# <class 'bool'>

# The type() function with a tuple variable
y = (1, 2, 3)
print(type(y)) 
# Output:
# <class 'tuple'>

# The type() function with a set variable
z = {1, 2, 3}
print(type(z)) 
# Output:
# <class 'set'>

3. isinstance() – Determine The Type of a Variable

Besides the type() function, We can also use the isinstance() function to check the type of a variable. The isinstance() function takes two arguments: the variable you want to check and the data type you want to check against. It returns True if the variable is of the specified data type, and False otherwise.

See if a variable value is float data type:


# The isinstance() function with a float variable
x = 3.14
print(isinstance(x, float)) 
# True

4. type() vs isinstance() – Which one is Good?

We can use the type() and isinstance() functions to determine the type of a variable in Python, but they differ in their approach. The type() function returns the exact type of the object, whereas the isinstance() function checks whether the object is an instance of a specific class or any of its subclasses.

Now it depends on the specific case that you will be dealing with. If you want to find the type then you have to use the type() function. Otherwise, if you want to check the type against some data type, then the isinstance() function will be a good fit.

5. __class__ Attribute – Determine the Variable Type

Python provides a special attribute called __class__ that can be used to determine the type of a variable. This attribute returns the class of an instance, which you can use to determine the type of the variable.


x = 23
print(x.__class__) 
# class 'int'

y = "SparkByExamples"
print(y.__class__) 
# class 'str'

It is suggested not to use the __class__ attribute as the type() function is working fine to determine the type of the variable.

6. Summary and Conclusion

We have discussed several methods for determining the type of a variable in Python, including type(), isinstance(), and the __class__ attribute. type() is a reliable method that can determine the type of any object in Python and will work 99% of the time. I hope this article was helpful. Please leave a comment if you have any questions.

Happy Coding!