Given some variables and we have to determine and print their type (i.e. the data type of Python variables)?
给定一些变量,我们必须确定并打印它们的类型 (即Python变量的数据类型 )?
Example:
例:
Input:
var = 10
Output:
type(var) = <type 'int'>
Python type()方法 (Python type() Method)
type() returns the type of the variable.
type()返回变量的类型。
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'>
翻译自: https://www.includehelp.com/python/determine-type-of-a-variable.aspx