- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python float() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python ascii() Function
- Python pow() Function
- Python type() Function
- Python List Functions
- Python list() Function
- Python insert() Function
- Python append() Function
- Python extend() Function
- Python pop() Function
- Python remove() Function
- Python reverse() Function
- Python sort() Function
- Python sorted() Function
- Python Dictionary Functions
- Python dict() Function
- Python update() Function
- Python get() Function
- Python keys() Function
- Python setdefault() Function
- Python fromkeys() Function
- Python items() Function
- Python popitem() Function
- Python Tuple Function
- Python tuple() Function
- Python Set Functions
- Python set() Function
- Python frozenset() Function
- Python String Functions
- Python split() Function
- Python join() Function
- Python format() Function
- Python replace() Function
- Python Iterator Functions
- Python iter() Function
- Python min() Function
- Python max() Function
- Python sum() Function
- Python count() Function
- Python index() Function
- Python copy() Function
- Python clear() Function
- Python next() Function
- Python filter() Function
- Python enumerate() Function
- Python zip() Function
- Python reversed() Function
- Python Number Functions
- Python abs() Function
- Python bin() Function
- Python oct() Function
- Python hex() Function
- Python round() Function
- Python divmod() Function
- Python complex() Function
- Python File Handling Functions
- Python open() Function
- Python read() Function
- Python readable() Function
- Python readline() Function
- Python readlines() Function
- Python write() Function
- Python writable() Function
- Python writelines() Function
- Python close() Function
- Python seek() Function
- Python tell() Function
- Python flush() Function
- Python fileno() Function
- Python truncate() Function
- Python Class Functions
- Python object() Function
- Python property() Function
- Python getattr() Function
- Python setattr() Function
- Python hasattr() Function
- Python delattr() Function
- Python classmethod() Function
- Python staticmethod() Function
- Python issubclass() Function
- Python super() Function
- Python Misc Functions
- Python all() Function
- Python any() Function
- Python isatty() Function
- Python bool() Function
- Python callable() Function
- Python globals() Function
- Python locals() Function
- Python dir() Function
- Python id() Function
- Python isinstance() Function
- Python map() Function
- Python repr() Function
- Python slice() Function
- Python vars() Function
- Python Advance Functions
- Python help() Function
- Python hash() Function
- Python breakpoint() Function
- Python bytes() Function
- Python bytearray() Function
- Python memoryview() Function
- Python compile() Function
- Python eval() Function
- Python exec() Function
- Python Tutorial
- Python Tutorial
- Python Examples
- Python Examples
Python type() Function
The type() function in Python is used when we need to find the type of an object. For example:
x = 10 print(type(x)) x = "Python Programming" print(type(x)) x = [12, 32, 54, 54] print(type(x)) x = ('codes', 'cracker', 'dot', 'com') print(type(x))
The output will be:
<class 'int'> <class 'str'> <class 'list'> <class 'tuple'>
Python type() Function Syntax
The syntax of type() function in Python, is:
type(object, bases, dict)
where object refers to an object whose type we need to find. The bases parameter is used to specify the base classes using tuple. And the third parameter dict refers to a dictionary that is the namespace with the definitions for class.
Note: The first parameter is required, whereas the second and third parameters are optional.
The type() function returns the type of specified object, in case if there is only one (object) parameter is used or passed. Otherwise returns a new type, in case if all the three parameters are passed.
Python type() Function Example
Here is an example of type() function in Python:
x = [1, 2, 4, 5] if type(x) is list: print("The object 'x' is of 'list' type.") else: print("The object 'x' is not a 'list' type.") x = "codescracker dot com" if type(x) is str: print("\nThe object 'x' is of 'str' type.") else: print("\nThe object 'x' is not a 'str' type.") x = 12.434 if type(x) is float: print("\nThe object 'x' is of 'float' type.") else: print("\nThe object 'x' is not a 'float' type.")
The output is:
The object 'x' is of 'list' type. The object 'x' is of 'str' type. The object 'x' is of 'float' type.
Python type() Function with All Three Parameters
This program is created to demonstrate the type() function in Python, will its all three parameters:
x = type('Python Programming', (object,), dict(Day='Mon', Month='Dec')) print(type(x)) print(vars(x)) class CodesCracker: Day = 'Mon' Month = 'Dec' x = type('Python is Fun', (CodesCracker,), dict(Day='Mon', Month='Dec')) print(type(x)) print(vars(x))
The output is:
<class 'type'> {'Day': 'Mon', 'Month': 'Dec', '__module__': '__main__', '__dict__': <attribute '__dict__' of 'Python Programming' objects>, '__weakref__': <attribute '__weakref__' of 'Python Programming' objects>, '__doc__': None} <class 'type'> {'Day': 'Mon', 'Month': 'Dec', '__module__': '__main__', '__doc__': None}
Note: The vars() function returns the __dict__ attribute of specified object.
« Previous Function Next Function »