python判断数字_python判断变量是否为数字、字符串、列表、字典等
生活随笔
收集整理的這篇文章主要介紹了
python判断数字_python判断变量是否为数字、字符串、列表、字典等
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在實(shí)際寫程序中,經(jīng)常要對(duì)變量類型進(jìn)行判斷,除了用type(變量)這種方法外,還可以用isinstance方法判斷:
a = [1,2]
if 'list' in str(type(a)):
print('1')
else:
print('2')
a = 1
b = [1,2,3,4]
c = (1,2,3,4)
d = {‘a(chǎn)‘:1,‘b‘:2,‘c‘:3}
e = "abc"
if isinstance(a,int):
print "a is int"
else:
print "a is not int"
if isinstance(b,list):
print "b is list"
else:
print "b is not list"
if isinstance(c,tuple):
print "c is tuple"
else:
print "c is not tuple"
if isinstance(d,dict):
print "d is dict"
else:
print "d is not dict"
if isinstance(e,str):
print "d is str"
else:
print "d is not str"
總結(jié)
以上是生活随笔為你收集整理的python判断数字_python判断变量是否为数字、字符串、列表、字典等的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于猛犸象DNA细胞培养制成的肉丸来了
- 下一篇: 4GB+64GB起 真我将在印度推出Re