python数字类型介绍以及创建数字值对象引用、删除数字值引用操作实例

时间:2022-11-04 21:53:49
#coding=utf8
'''
数字类型:
数字提供标量贮存和直接访问。
它是不可更改类型,变更数字的值会产生新的对象。
Python支持多种数字类型:整型、长整型、布尔型、双精度浮点型、十进制浮点型和复数。
'''
def digtalIndroduce():
''' 创建数值对象并给其赋值 '''
print "The first give the variable to value"
Int=45
Long=4444444444444L
Bool=True
Float=3.141592653589793228885555558888
Dicmal=10.25
Complex=25+12.36j

print "%d the id:%r" %(Int,id(Int))
print "%r the id:%r" %(Long,id(Long))
print "%r the id:%r" %(Bool,id(Bool))
print "%r the id:%r" %(Float,id(Float))
print "%r the id:%r" %(Dicmal,id(Dicmal))
print "%r the id:%r" %(Complex,id(Complex))
'''
更新数字对象:
通过给数字对象重新赋值,可以“更新”一个数值对象。
所谓的更新,其实就是生成一个新的数值对象,并得到它的引用。
'''
print "Update the value of the variable"
Int+=5
Long=4444L
Bool=False
Float=3.14188
Dicmal+=10.25
Complex=10+12j

print "%d the id:%r" %(Int,id(Int))
print "%r the id:%r" %(Long,id(Long))
print "%r the id:%r" %(Bool,id(Bool))
print "%r the id:%r" %(Float,id(Float))
print "%rthe id:%r" %(Dicmal,id(Dicmal))
print "%r the id:%r" %(Complex,id(Complex))

'''
删除数字对象:
Python无法真正删除一个数值对象,仅仅删除数值对象的一个引用。
删除对象的引用之后,就不可以再使用这个引用(变量名),除非重新赋值。
否则使用删除的引用,会引发NameError异常。
'''
print "delete the object reference"
del Int,Long,Bool,Float,Dicmal,Complex
try:
print "%d the id:%r" %(Int,id(Int))
print "%r the id:%r" %(Long,id(Long))
print "%r the id:%r" %(Bool,id(Bool))
print "%r the id:%r" %(Float,id(Float))
print "%rthe id:%r" %(Dicmal,id(Dicmal))
print "%r the id:%r" %(Complex,id(Complex))
except NameError,e:
print "NameError:",e

'''call the function :digtalIndroduce()'''
digtalIndroduce()