#!/usr/bin/env python
def displayNumType(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!'
displayNumType(-69)
displayNumType(9999999L)
displayNumType(98.6)
displayNumType(-5.2+1.9j)
displayNumType('xxx')
标识符检查(idcheck.py)
#!/usr/bin/env python
import string
alphas = string.letters + '_'
nums = string.digits
print 'Welcome to the Identfier Checker v1.0'
print 'Testees must be at least 2 chars long.'
myInput = raw_input("Identifier to test ? ")
if len(myInput) > 1:
if myInput[0] is not in alphas:
print '''invalid: first symbol must be
alphabetic'''
else:
for otherChar in myInput[1:]:
if otherChar not in alpha + nums:
print '''invalid: remaining
symbols must be alphanumeric'''
break
else:
print ''okay as an identifier''
#一般来说,从性能角度来考虑,把重复操作作为参数放到循环里面进行是非常低效的。可以考虑把重复进行运算操作的值做一个变量赋值保存,后续直接引用变量。