Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1710296
  • 博文数量: 362
  • 博客积分: 10587
  • 博客等级: 上将
  • 技术积分: 4098
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-10 18:15
文章分类

全部博文(362)

文章存档

2014年(1)

2013年(58)

2011年(115)

2010年(112)

2009年(76)

分类: Python/Ruby

2009-10-29 16:14:29

"Display number type"

def disnumtype(num):
    print num,'is',
    if isinstance(num,(int,long,float,complex)):
        print 'a number of type:',type(num).__name__
    else:
        print 'not a number at all!!'

 

比如保存为名字: disnumtype.py

则执行的时候为

>>>disnumtype(5)

>>>disnumtype('abc')

 

 

或者如下的程序:

 

def disnumtype(num):
    print num,"is",
    if type(num) == type(0):
        print 'an integer'
    elif type(num) == type(0L):
        print 'a long'
    elif type(num) == type(0.0):
        print 'a float'
    elif type(num) == type(0+0j):
        print 'a complex number'
    else:
        print 'not a number at all!'

   

阅读(1074) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~