PythonTip >> 博文 >> python

python学习笔记之 数据类型

zihua 2014-02-20 18:02:42 点击: 801 | 收藏


#_*_coding:utf-8_*_
class Foo:pass
class Boo(object):pass
foo =Foo()
boo =Boo()
'''type()
	返回任意Python 对象对象的类型
'''
print type(4)
print type('hello world')
print type(0+0j)
print type(0L)
print type(0.0)
print type([])
print type(())
print type({})
print type(type(4))
print type(boo)
print type(Foo)
print type(Boo)
'''id获取的只是接近指针的一个地址
    python中处处是指针
    但是在这里没有意义
'''
x="my python ok"
print id(x)
print str(4.53-2j)
'''cmp	对象比较'''
a,b=-4,12
print cmp(a,b)
print cmp(b,a)
b=-4
print cmp(a,b)

''' str和repr
    str()一般是将数值转成字符串。 
	repr()是将一个对象转成字符串显示,注意只是显示用,有些对象转成字符串没有直接的意思。如list,dict使用str()是无效的,但使用repr可以,这是为了看它们都有哪些值,为了显示之用。		  
'''
print str([0, 5, 9, 9])
print repr([0, 5, 9, 9])
print str('0.1')
print repr('0.1')

输出结果

<type 'int'>
<type 'str'>
<type 'complex'>
<type 'long'>
<type 'float'>
<type 'list'>
<type 'tuple'>
<type 'dict'>
<type 'type'>
<class '__main__.Boo'>
<type 'classobj'>
<type 'type'>
4800008
(4.53-2j)
-1
1
0
[0, 5, 9, 9]
[0, 5, 9, 9]
0.1
'0.1'



原文链接:http://my.oschina.net/dlpinghailinfeng/blog/201546

作者:zihua | 分类: python | 标签: python | 阅读: 801 | 发布于: 2014-02-20 18时 |