beginnerPython
How do you check the type of a variable?
Answer
Clear, interview-ready explanation
The function returns the exact type of an object:
type()
age = 25
name = "Aman"
print(type(age))
print(type(name))
Output:
<class 'int'>
<class 'str'>
The function checks whether an object belongs to a particular class or any of its subclasses:
isinstance()
age = 25
print(isinstance(age, int))
print(isinstance(age, str))
Output:
True
False