How to check variable type in python

Hello guys. Ho are you all? Hope you all fine. In this tutorial we will learn about How to check variable type in python. In python there are so many type of variable. So for to know the type of the variable there are some methods which we are going to learn about. These methods describe the type of the variable and how we can know the type of a variable. So lets learn guys. I hope this will help you to learn it.

Type of the variable:

  1. integer : It is store the data of the numbers. It only contains number in it.
  2. float : It store floating point numbers.
  3. List : It store the list of the elements.
  4. set : It store the set of the elements.
  5. dictionary : It store the dictionary of the elements.
  6. string : It store the character in it.
  7. tuple : It store the tuple of elements in it.

So for knowing the type of the variable lets learn this methods. From these methods you can easily find out the type of the variable.

How to check variable type in python

  1. check variable type in python

    to check variable type in python just Use type().The function type() used to check the type of a variable. type() take the variable as an input and it will return the type of the variable. Here i am giving an example of it. Lets learn by this to check the variable in python. myInt = 25 print(type(myInt)) myFloat = 91.40 print(type(myFloat)) myString = "Good morning" print(type(myString)) mydict = {"fruit":"mango"} print(type(mydict)) mydict = {"fruit":"mango"} print(type(mydict)) mylist = [1, 2, 3, 4] print(type(mylist)) mytuple = (12,15,18) print(type(mytuple)) Output : <class 'int'> <class 'float'> <class 'str'> <class 'dict'> <class 'dict'> <class 'list'> <class 'tuple'>

  2. How to check variable type in python

    to check variable type in python just Use isinstance(). By using isinstance() you can check the type of the variable in python. isinstance() takes input and the return it is true or not. By using it you can check the type of a variable. Lets learn this by an example: var1 = isinstance(45,int) print("var1 is an integer:", var1) var2 = isinstance(10.45,int) print("var1 is an integer:", var2) var3 = isinstance(10.12,float) print("var3 is float:", var3) var4 = isinstance("hello",str) print("var4 is string:", var4) var5 = isinstance((1,2,3,4,5),tuple) print("var5 is a tuple:", var5) var6 = isinstance({1,2,3,4,5},set) print("var6 is a set:", var6) var7 = isinstance([1,2,3,4,5],list) print("var7 is a list:", var7) Output :
    var1 is an integer: True var1 is an integer: False var3 is float: True var4 is string: True var5 is a tuple: True var6 is a set: True var7 is a list: True

Method 1: Use type()

The function type() used to check the type of a variable. type() take the variable as an input and it will return the type of the variable. Here i am giving an example of it. Lets learn by this to check the variable in python.

myInt = 25
print(type(myInt))

myFloat = 91.40
print(type(myFloat))

myString = "Good morning"
print(type(myString))

mydict = {"fruit":"mango"}
print(type(mydict))

mydict = {"fruit":"mango"}
print(type(mydict))

mylist = [1, 2, 3, 4]
print(type(mylist))

mytuple = (12,15,18)
print(type(mytuple))

Output :

<class 'int'>
<class 'float'>
<class 'str'>
<class 'dict'>
<class 'dict'>
<class 'list'>
<class 'tuple'>

Method 2: Use isinstance()

By using isinstance() you can check the type of the variable in python. isinstance() takes input and the return it is true or not. By using it you can check the type of a variable. Lets learn this by an example:

var1 = isinstance(45,int)
print("var1 is an integer:", var1)

var2 = isinstance(10.45,int)
print("var1 is an integer:", var2)

var3 = isinstance(10.12,float)
print("var3 is float:", var3)

var4 = isinstance("hello",str)
print("var4 is string:", var4)

var5 = isinstance((1,2,3,4,5),tuple)
print("var5 is a tuple:", var5)

var6 = isinstance({1,2,3,4,5},set)
print("var6 is a set:", var6)

var7 = isinstance([1,2,3,4,5],list)
print("var7 is a list:", var7)

Output :

var1 is an integer: True
var1 is an integer: False
var3 is float: True
var4 is string: True
var5 is a tuple: True
var6 is a set: True
var7 is a list: True

Method 3:

Output :

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment