给定一些变量,我们必须确定并打印它们的类型(即Python变量的数据类型)?
例子:
Input: var = 10 Output: type(var) = <type 'int'>
type() 返回变量的类型。
语法:
type(variable_name)
程序:
# 声明不同类型的变量 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 "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)
输出
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'>