Python type() Function with Examples

type() Function in Python:

The type() function returns the type of the object that was passed to it.

Based on the arguments passed, the type() function either returns the type of the object or returns a new type object.

Syntax:

type(object, base, dict)

Parameters

object: This is required. The type() function returns the type of this object if only one parameter is specified.

base: This is optional. It Describes or specifies the base classes.

dict: This is optional. Specifies the namespace containing the class definition.

Return Value:

This function returns

  • If only one object parameter is passed, the type of the object is returned.
  • Returns a new type, if three parameters are passed.

Examples:

Example1:

Input:

Given number = 30
Given string = "hello this is btechgeeks"
Given list = [1, 4, 5, 6]

Output:

The type of given number =  <class 'int'>
The type of given string =  <class 'str'>
The type of given list =  <class 'list'>

type() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Give the string as static input and store it in another variable.
  • Give the list as static input and store it in another variable.
  • Pass the given number as an argument to the type() function that returns the type of the object that was passed to it.
  • Store it in another variable.
  • Print the result after applying the type() function on the given number.
  • Similarly, do the same for the given string and list and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 30
# Give the string as static input and store it in another variable.
gvn_str = "hello this is btechgeeks"
# Give the list as static input and store it in another variable.
gvn_lst = [1, 4, 5, 6]
# Pass the given number as an argument to the type() function that returns the 
# type of the object that was passed to it.
# Store it in another variable.
rslt1 = type(gvn_numb)
# Print the result after applying the type() function on the given number.
print("The type of given number = ", rslt1)
# similarly do the same for the given string and list and print it.
rslt2 = type(gvn_str)
print("The type of given string = ", rslt2)
rslt3 = type(gvn_lst)
print("The type of given list = ", rslt3)

Output:

The type of given number =  <class 'int'>
The type of given string =  <class 'str'>
The type of given list =  <class 'list'>

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number as user input using the float(input()) function and store it in a variable.
  • Give the string as user input using the input() function and store it in another variable.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given number as an argument to the type() function that returns the type of the object that was passed to it.
  • Store it in another variable.
  • Print the result after applying the type() function on the given number.
  • Similarly, do the same for the given string and list and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using the float(input()) function and store it in a variable.
gvn_numb = float(input("Enter some random number = "))
# Give the string as user input using the input() function and 
# store it in another variable.
gvn_str = input("Enter some random string = ")
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Pass the given number as an argument to the type() function that returns the 
# type of the object that was passed to it.
# Store it in another variable.
rslt1 = type(gvn_numb)
# Print the result after applying the type() function on the given number.
print("The type of given number = ", rslt1)
# similarly do the same for the given string and list and print it.
rslt2 = type(gvn_str)
print("The type of given string = ", rslt2)
rslt3 = type(gvn_lst)
print("The type of given list = ", rslt3)

Output:

Enter some random number = 50.5
Enter some random string = good morning btechgeeks
Enter some random List Elements separated by spaces = 3 2 4 1
The type of given number = <class 'float'>
The type of given string = <class 'str'>
The type of given list = <class 'list'>