Getting type of python variable

type_of_variable

Skill - Getting type of variable in python

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision

Main Code

x = 5
print(type(x))
# <class 'int'>

y = 'Nagasudhir'
print(type(y))
# <class 'str'>

z = [1, 3, 6]
print(type(z))
# <class 'list'>

Check the type of variable using isinstance function

x = 5
print(isinstance(x, int))
# prints True

y = 3.2
print(isinstance(y, float))
# prints True

z = ['abc', 23, 7.6]
print(isinstance(z, list))
# prints True

k = 5.5
# check if variable is one of the specified types
print(isinstance(k, (int, float)))
# prints True

Online Interpreter

You can run these codes online at https://www.programiz.com/python-programming/online-compiler/


Table of Contents

Comments