What are the differences between type() and isinstance()?

In Python, type() is a built-in function that returns the type of an object, while isinstance() is a function that checks whether an object is an instance of a particular class or of a subclass thereof.

Here's an example of how to use these functions:

x = 5
print(type(x))  # Output: <class 'int'>
print(isinstance(x, int))  # Output: True

The type() function returns the type of the object as a type object (for example, int, float, str, etc.). On the other hand, isinstance() checks the object's type and returns True if it is an instance of the specified class or a subclass thereof, and False otherwise.

For example:

class MyClass:
  pass

x = MyClass()

print(type(x))  # Output: <class '__main__.MyClass'>
print(isinstance(x, MyClass))  # Output: True

In this case, type(x) returns the type of x as MyClass, while isinstance(x, MyClass) returns True because x is an instance of the MyClass class.

One important thing to note is that type() only works for objects of type type or str, while isinstance() works for any object.

I hope this helps! Let me know if you have any questions.