Menu Close

Python type() Function

python type function

In this article, we are going to learn all about the Python type function to find the type of any python object. type function in Python is a very useful function to find the data type of any python object. In previous python built-in functions tutorials, we have seen various Python built-in functions along with examples.

Python type function Introduction

Python type function is a built-in function in Python that is used to find the type of Python object. To use the type function in Python, you don’t need to install it by using the Python pip command because it comes with Python by default.

Syntax:

The syntax of the type function is:-

type(object, base, dict)

Parameter:

type function in Python accepts three parameters:-

  • object:- Required, If one parameter is specified, the type function returns the type of this object.
  • base:- Optional. Specifies the base classes.
  • dict:- Optional, Specify the namespace with the definition of the class.

Return value:

The return value of the Python type function is a type of object.

Python type() function Examples

Here we will see multiple examples of the type() function so that you can get more clarity about the Python type() function.

Example: Use the type() function to find the type of Python object.


print("Type of string:- ", type("programming"))
print("Type of list:- ", type([1, 2, 3, 4, 5, 6, 7, 8]))
print("Type of set:- ", type({1, 2, 3, 4, 5, 6, 7, 8}))
print("Type of tuple:- ", type((1, 2, 3, 4, 5, 6, 7, 8)))

my_dict = {"name": "Vishvajit", "age": 22}
print("type of dictionary: ", type(my_dict))

Output

Type of string:-  <class 'str'>
Type of list:-  <class 'list'>
Type of set:-  <class 'set'>
Type of tuple:-  <class 'tuple'>
type of dictionary:  <class 'dict'>

Example:- Get the type of user-defined class object

In this example, we are about to get the type of user-defined class object.


class Addition:

    def __init__(self, a, b):
        self.a = a
        self.b = b

    def addition(self):
        if self.a and self.b:
            return f"Addition of {self.a} and {self.b} will be:- {self.a + self.b}"
        else:
            return print("Please, enter valid numbers")


result = Addition(12, 120)
print(result.addition())

print(type(result))

Output

Addition of 12 and 120 will be:- 132
<class '__main__.Addition'>

Example: Using type() function with three parameters

As we know that type function in Python accepts three parameters, object, base and dict. Let’s understand python type() function by passing these all three parameters.


x = type(
    "Testing",
    (object,),
    dict(first_name="Programming", last_name="Funda", full_name="ProgramingFunda"),
)

# type of x
print(type(x))

# attribute of object x
print(vars(x))

Output

<class 'type'>
{'first_name': 'Programming', 'last_name': 'Funda', 'full_name': 'ProgramingFunda', '__module__': '__main__', '__dict__': <attribute '__dict__' of 'Testing' objects>, '__weakref__': <attribute '__weakref__' of 'Testing' objects>, '__doc__': None}

Code Explanation

Steps which are comprises in above example.

  • Created a type objevt by passing three parameters in Python type function. We passed Testing as name of the object, it base as an object and initialize an dictionary having three keys and values.
  • The first print(type(x)) statement will print the class object as a type and second print statement print(vars(x)) will return all the attributes of the object x.

Conclusion:

So, In this article, we have seen all about Python type() function along with various examples. The type function is very useful when you want to find the type of any python object.

If you like this article, please share and keep visiting for further Python tutorials.

Python built-in functions


For more information:- Click Here

Thanks for your valuable time….🧑‍💻🧑‍💻🙏🙏

Python bin() Function
Python zip() function

Related Posts