MWZ

MINDWAREZONE

beginnerPython

How do you check the type of a variable?

Answer

Clear, interview-ready explanation

The type() function returns the exact type of an object:

age = 25
name = "Aman"

print(type(age))
print(type(name))
            

Output:

<class 'int'>
<class 'str'>
            

  The isinstance() function checks whether an object belongs to a particular class or any of its subclasses:

age = 25

print(isinstance(age, int))
print(isinstance(age, str))
            

Output:

True
False