In Python, the type()
function is used to determine the type of an object. It returns the data type of the specified object. The syntax for using the type()
function is as follows:
type(object)
Here, object
is the Python object whose type you want to check.
Let's see some examples:
num = 42
print(type(num)) # Output: <class 'int'>
text = "Hello, World!"
print(type(text)) # Output: <class 'str'>
my_list = [1, 2, 3, 4, 5]
print(type(my_list)) # Output: <class 'list'>
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(type(my_dict)) # Output: <class 'dict'>
def my_function():
return "Hello, from a function!"
print(type(my_function)) # Output: <class 'function'>
Using the type()
function can be helpful in situations where you need to perform different operations or checks based on the type of an object. Keep in mind that the type()
function returns a type object, and you can use it to compare types or perform other operations as needed.