1.python常见的数据类型:
数字:整型、长整型、浮点数、复数、科学计数法字符串:字节字符串、unicode字符串List(列表)Tuple(元组)Dictionary(字典)bool(布尔)
>>> a =1
>>> b =2L
>>> c=1.2
>>> d=1+1j
>>> e="s"
>>> f=u"f"
>>> g=[]
>>> h=(1,)
>>> i={1:2}
>>> True
True
>>> type(a)
<type 'int'>
>>> type(b)
<type 'long'>
>>> type(c)
<type 'float'>
>>> type(d)
<type 'complex'>
>>> type(e)
<type 'str'>
>>> type(f)
<type 'unicode'>
>>> type(g)
<type 'list'>
>>> type(h)
<type 'tuple'>
>>> type(i)
<type 'dict'>
>>> type(True)
<type 'bool'>
1.2. 判断数据类型:
>>> isinstance(1,(int,long,str))
True
>>> isinstance(1,int)
True
>>> type(open("e:\\a.txt"))
<type 'file'> #文件类型
>>> def sum():pass
...
>>> type(sum)
<type 'function'>
>>> 1e10 #科学计数法
10000000000.0
>>> 1e-3
0.001
>>> 1e2+100
200.0
>>> True
True
>>> False
False
1.3. 判断是否是字符串:
#-*- coding: UTF-8 -*-
s = "hello normal string"
u=u"unicode"
if isinstance( s, basestring ):
print u"是字符串"
if isinstance( u, basestring ):
print u"是字符串"
>>> isinstance(u"s",str)
False
>>> isinstance(u"s",unicode)
True
>>> isinstance(u"s",basestring)
True
>>> isinstance("s",str)
True
>>> isinstance("s",unicode)
False
>>> isinstance("s",basestring)
True