Python | 确定变量的类型

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值