A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/python/python_type_function.htm below:

Python type() Function

Python type() Function

The Python type() function is a built-in function that returns the type of specified object and is commonly used for debugging purposes. The two main use cases of the type() function are as follows −

Syntax

Following is the syntax of the Python type() function −

type(object, bases, dict)
# or
type(object)
Parameters

The Python type() function accepts two parameters −

Return Value

The Python type() function returns the type of a given object. If we pass three arguments, it will return a new type object.

type() Function Examples

Practice the following examples to understand the use of type() function in Python:

Example: Use of type() Function

The following example shows the basic usage of Python type() function. In the code below, we are displaying the classes for the most used data types.

var1 = 5
var2 = 5.2
var3 = 'hello'
var4 = [1,4,7]
var5 = {'Day1':'Sun','Day2':"Mon",'Day3':'Tue'}
var6 = ('Sky','Blue','Vast')
print(type(var1))
print(type(var2))
print(type(var3))
print(type(var4))
print(type(var5))
print(type(var6))

Following is an output of the above code −

<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>
<class 'dict'>
<class 'tuple'>
Example: Getting Type of Classes Using type() Function

The class named "type" is a superclass, from which all other classes are derived. The type object is also an instance of this class. Therefore, applying type() function to the Python classes will return "class type" as a result.

print("Displaying the type of Python classes:")
print("Type of int class is", type(int))
print("Type of dict class is", type(dict))
print("Type of list class is", type(list))
print("Type of type class is", type(type))

Output of the above code is as follows −

Displaying the type of Python classes:
Type of int class is <class 'type'>
Type of dict class is <class 'type'>
Type of list class is <class 'type'>
Type of type class is <class 'type'>
Example: Creating New Type Object Using type() Function

If we pass three arguments to the type() function, it will create a new type object. In the code below, we have created "Tutorialspoint" as a main class, an object as a base and a dictionary with two keys and values.

Object = type("Tutorialspoint", (object, ), dict(loc="Hyderabad", rank=1))
print(type(Object))
print(Object)

Following is the output of the above Python code −

<class 'type'>
<class '__main__.Tutorialspoint'>

python_built_in_functions.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4