How can I determine the type of an object in Python?

How can I determine the type of an object in Python?

I want to know if there’s a simple way to check if a variable is a list, dictionary, or some other type of object. For example, how can I check the type of a variable using Python typeof?

The type() function is a simple way to check the type of an object. It returns the type of the object as a class type.

Example:

my_var = [1, 2, 3]
print(type(my_var))  # Output: <class 'list'>

The isinstance() function checks if an object is an instance of a particular class or a tuple of classes. This is useful for verifying the type of an object, especially when dealing with inheritance.

Example:

my_var = {'a': 1, 'b': 2}
print(isinstance(my_var, dict))  # Output: True

If you need to identify both the type and the memory address (unique identifier) of an object, you can use id() along with type(). Example:

my_var = "Hello, world!"
print(id(my_var))  # Output: Unique memory address of the object
print(type(my_var))  # Output: <class 'str'>