Python type() Function
Overview
The type() function in Python is used to determine the type of an object. This function is extremely useful for debugging and during development, as it lets you understand what kind of data you’re working with at any point in your code.
type() Function Syntax
The basic syntax of the function is as follows:
>> type(object)
object: The item whose type you want to determine.
Return Value: The function returns the type of the object passed to it.
Examples
Some examples demonstrating the function use:
>>> x = 7
>>> type(x)
<class ‘int’>
>>>
>>> pi = 3.14
>>> type(pi)
<class ‘float’>
>>>
>>> clx = 7 + 9j
>>> type(clx)
<class ‘complex’>
>>>
>>> s = “TestingDocs”
>>> type(s)
<class ‘str’>
>>>
>>>my_list = [1, 2, 3]
>>>type(my_list)
<class ‘list’>
The type() function is especially useful in dynamic typing situations where the type of an object can change, and you need to ensure it is the type you expect. It’s also commonly used in conjunction with isinstance() for more complex type checking.
—
Python Tutorials
Python Tutorial on this website can be found at:
https://www.testingdocs.com/python-tutorials/
Official Website
More Information on Python: