Home » Python

Python | Determine type of a variable

In this article, we are going to learn how to determine the type of a variable in Python? To determine the type of a variable - we use type() method.
Submitted by IncludeHelp, on August 14, 2018

Given some variables and we have to determine and print their type (i.e. the data type of Python variables)?

Example:

    Input:
    var = 10

    Output:
    type(var) = <type 'int'>

Python type() Method

type() returns the type of the variable.

Syntax:

    type(variable_name)

Program:

# declare different types of variables
var1 = None
var2 = 10
var3 = 20.20
var4 = "Hello world!"
var5 = [10,20,30]
var6 = ['Hello', 'world']
var7 = [ [10,20], [30,40], [50,60] ]
var8 = True
var9 = (1, 'AmitShukla', 21)
var10 = {10, 20, 30}
var11 = {1:'Amit', 2: 'Abhishek'}

# print variables type 
print "type (var1) : ", type (var1)
print "type (var2) : ", type (var2)
print "type (var3) : ", type (var3)
print "type (var4) : ", type (var4)
print "type (var5) : ", type (var5)
print "type (var6) : ", type (var6)
print "type (var7) : ", type (var7)
print "type (var8) : ", type (var8)
print "type (var9) : ", type (var9)
print "type (var10): ", type (var10)
print "type (var11 : ", type (var11)

Output

    type (var1) :  <type 'NoneType'>
    type (var2) :  <type 'int'>
    type (var3) :  <type 'float'>
    type (var4) :  <type 'str'>
    type (var5) :  <type 'list'>
    type (var6) :  <type 'list'>
    type (var7) :  <type 'list'>
    type (var8) :  <type 'bool'>
    type (var9) :  <type 'tuple'>
    type (var10):  <type 'set'>
    type (var11 :  <type 'dict'>





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




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.