close

How to check variable type in python

Variable is one type of box which store the data. The variable can be any from any of the data type. there are so many data types. We will learn about How to check the variable type in python. So lets learn about the different data type in python. and how we can recognize them by these methods. Lets learn about of this.

How to check variable type in python

type of the variables:

  1. integer : Integer is the data type which is containing the numbers.
  2. Float : It is contain the floating point numbers.
  3. String : It contains character and symbols from languages.
  4. List : It contain collection of the elements.
  5. tuple: This is also collection of element. But it is immutable.
  6. Dictionary : collection of key value pairs.
  7. Set : It is the collection of unique items.

In this tutorial we will learn about to check this variable. In python language there are so many variable uses. So here i m giving you some methods which will help you to recognize the variable type. So lets learn this.

  1. check variable type in python

    to check variable type in python just Use type().Just use the type() function to recognize the data type or variable which you want to check. Its very simple and easy way to use. Lets learn this by given below example. var1 = 85 print(type(var1)) var2 = 91.80 print(type(var2)) var3 = "Good morning" print(type(var3))Output :<class 'int'> <class 'float'> <class 'str'>
    There are another example of different variable . lets see this.
    var1 = {'mango':'fruit'} print(type(var1)) var2 = [11, 12, 13] print(type(var2)) tuple = (10,20,30) print(type(tuple))Output : <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 variable type. Here you have to pass two thing variable and second is variable type. Then you can check the type you entered is true or false. lets see this by example: Var1 = 50 print("My var is int : ",isinstance(Var1, int)) print("My var is int: ",isinstance("My String", int)) Output : My var is int : True My var is int: False

Method 1: Use type()

Just use the type() function to recognize the data type or variable which you want to check. Its very simple and easy way to use. Lets learn this by given below example.

var1 = 85
print(type(var1))

var2 = 91.80
print(type(var2))

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

Output :

<class 'int'>
<class 'float'>
<class 'str'>

There are another example of different variable . lets see this.

var1 = {'mango':'fruit'}
print(type(var1))

var2 = [11, 12, 13]
print(type(var2))

tuple = (10,20,30)
print(type(tuple))

Output :

<class 'dict'>
<class 'list'>
<class 'tuple'>

Method 2: Use isinstance()

By using isinstance() you can check the variable type. Here you have to pass two thing variable and second is variable type. Then you can check the type you entered is true or false. lets see this by example:

Var1 = 50
print("My var is int : ",isinstance(Var1, int))
print("My var is int: ",isinstance("My String", int))

Output :

My var is int :  True
My var is int:  False

Conlusion

Hope all 6 Method Are Useful For You. Comment Below Which Method You Used To Convert String To Datetime In Python. Thank You.

Also Read

Leave a Comment