ADVERTISEMENT
ADVERTISEMENT

Determine the type of an object in Python

Here, we are going to learn how to determine the type of object in Python? Here, we are implementing a program which is an example of type() function in python.
Submitted by IncludeHelp, on April 26, 2019

Given some of the objects in python, we have to determine their types.

To determine the type of an object, we use type() function – which is a built-in function in python.

Python type() function

type() function is used to determine the type of an object, it accepts an object or value and returns it's type (i.e. a class of the object).

Syntax:

    type(object)

Example:

    Input:
    b = 10.23
    c = "Hello"

    # Function call
    print("type(b): ", type(b))
    print("type(c): ", type(c))

    Output:
    type(b):  <class 'float'>
    type(c):  <class 'str'>

Python code to determine the type of objects

# Python code to determine the type of objects

# declaring objects and assigning values
a = 10
b = 10.23
c = "Hello"
d = (10, 20, 30, 40)
e = [10, 20, 30, 40]

# printing types of the objects
# using type() function
print("type(a): ", type(a))
print("type(b): ", type(b))
print("type(c): ", type(c))
print("type(d): ", type(d))
print("type(e): ", type(e))

# printing the type of the value
# using type() function
print("type(10): ", type(10))
print("type(10.23): ", type(10.23))
print("type(\"Hello\"): ", type("Hello"))
print("type((10, 20, 30, 40)): ", type((10, 20, 30, 40)))
print("type([10, 20, 30, 40]): ", type([10, 20, 30, 40]))

Output

type(a):  <class 'int'>
type(b):  <class 'float'>
type(c):  <class 'str'>
type(d):  <class 'tuple'>
type(e):  <class 'list'>
type(10):  <class 'int'>
type(10.23):  <class 'float'>
type("Hello"):  <class 'str'>
type((10, 20, 30, 40)):  <class 'tuple'>
type([10, 20, 30, 40]):  <class 'list'>

Python Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.