Introduction
Python has many built-in functions. In this tutorial, we will be discussing how to check the data-type of the variables in python by using type(). As, while programming in Python, we came to a situation where we want to check the data-type of the variable we use type() function. This article will help you understand the concept of type() function.
What is type() function?
Python type() is a built-in function that helps you find the class type of the variable given as input. You have to just place the variable name inside the type() function, and python returns the datatype.
Mostly, We use it for debugging purposes. we can also pass three arguments to type(), i.e., type(name, bases, dict). In such a case, it will return you a new type of object.
Syntax
type(object)
Parameter
The object argument is the required parameter to be passed inside the type() function. The argument can be string, integer, list, tuple, set, dictionary, float, etc.
Syntax
type(name, bases, dict)
Parameter
- name: It is the name of the class.
- bases: It is the optional parameter, and it is the name of the base class.
- dict: It is an optional parameter, and it is the namespace that has a definition of the class.
Return value
- If we pass only the object as the parameter, it will only return the object’s type.
- If we pass the name, bases, and dict as the parameter, it will return the new type.
Examples to Check Data Type in Python
Let us discuss certain ways through which we can print the datatype of the variable.
1. Using type(object) Method to Check Data Type in Python
In this example, we will be taking the input in all the forms to write the variable like string, integer, negative value, float value, complex number, list, tuple, set, and dictionary. After that, we will be printing the data type of all the variables and see the output.
#taking input
str = 'Python pool'
print('Datatype : ',type(str))
num = 100
print('Datatype : ',type(num))
neg = -20
print('Datatype : ',type(neg))
float = 3.14
print('Datatype : ',type(float))
complex = 2 + 3j
print('Datatype : ',type(complex))
lst = [1,2,3,4,5]
print('Datatype : ',type(lst))
Tuple = (1,2,3,4,5)
print('Datatype : ',type(Tuple))
Dict = {"a" : 2, "b" :3, "c" :1}
print('Datatype : ',type(Dict))
set = {'a', 'b', 'c', 'd'}
print('Datatype : ',type(set))
Output:
Datatype : <class 'str'>
Datatype : <class 'int'>
Datatype : <class 'int'>
Datatype : <class 'float'>
Datatype : <class 'complex'>
Datatype : <class 'list'>
Datatype : <class 'tuple'>
Datatype : <class 'dict'>
Datatype : <class 'set'>
Explanation:
- We have taken input as a string.
- Then applied the type of string and printed the output.
- We have taken input as a number.
- Then applied the type of number and printed the output.
- We have taken input as a negative value.
- Then applied the type of negative value and printed the output.
- We have taken input as a float value.
- Then applied the type of float value and printed the output.
- We have taken input as a complex value.
- Then applied the type of complex value and printed the output.
- We have taken input as a list.
- Then applied the type of list and printed the output.
- We have taken input as a tuple.
- Then applied the type of tuple and printed the output.
- We have taken input as a dictionary.
- Then applied the type of dictionary and printed the output.
- We have taken input as a set.
- Then applied the type of set and printed the output.
2. Using type(name, bases, dict) method to Check Data Type in Python
In this example, we will be taking all the parameters like name, bases, and dict. after that, we will print the output. Let see more clearly with the help of the program.
class Python:
x = 'python pool'
y = 100
t1 = type('NewClass', (Python,), dict(x='Python pool', y=100))
print(type(t1))
print(vars(t1))
Output:
<class 'type'>
{'x': 'Python pool', 'y': 100, '__module__': '__main__', '__doc__': None}
Explanation:
- Firstly, we have taken a class, Python.
- Then, we have taken a string and integer value inside the class python.
- Then, we have taken a variable as t1 in which we have applied the type() with the parameter as name, bases, and dict
- After that, we have printed the type of t1 and vars of t1.
- At last, you can see the output.
Difference between type() and isinstance()
Type() Function
Python type() is a built-in function that helps you find the class type of the variable given as input.
Isinstance() Function
Python isinstance() function is used to check if the object (first argument) is an instance or subclass of classinfo class (second argument).
Example of type() and isinstance() function
In this example, we will be discussing about both the functions and explained in detail.
age = 100
print("Datatype : ",type(age))
age = isinstance(100,int)
print("age is an integer:", age)
Output:
Datatype : <class 'int'>
age is an integer: True
Explanation:
- Firstly, we have taken age as a variable with a value equal to 100.
- Then, we have printed the datatype of the age value.
- After that, we have applied
isinstance()
function with two parameters as the value of age and type of age. - At last, we have printed the output.
- By seeing the output, we can see the difference between the type and
isinstance
function.
Conclusion
In this tutorial, we have learned how to check the variable’s data type by using type() with two different parameters. We have also explained all the variables using type() with examples explained in detail.
However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.
The post Type() Function | How to Check Data Type in Python appeared first on Python Pool.
from Planet Python
via read more
No comments:
Post a Comment