type() Function in Python

Python type() Function

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:

Checking the type of a number

num = 42
print(type(num))  # Output: <class 'int'>

Checking the type of a string

text = "Hello, World!"
print(type(text))  # Output: <class 'str'>

Checking the type of a list

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

Checking the type of a dictionary

my_dict = {'a': 1, 'b': 2, 'c': 3}
print(type(my_dict))  # Output: <class 'dict'>

Checking the type of a function

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.