python --数学函数记录

#!/usr/bin/python
# -*- coding:utf-8 -*-

import sys
import math

# 判断是否为数字
def isNum(value):
    try:
        value+1
    except TypeError:
        return False
    else:
        return True

# 判断是否为数字
def isFloat(value):
    try:
       f = float(value)
    except TypeError:
        return False
    except ValueError:
        return False
    except Exception, e:
        return False
    else:
        return f

def test1():
    a = "123abcDE"
    print a.isalnum()       # True, 所有字符都是数字或者字母
    a = "abcDE"
    print a.isalpha()       # True, 所有字符都是字母
    a = "123.3"
    print a.isdigit()       # False, 所有字符都是数字
    a = "abcde"
    print a.islower()       # True, 所有字符都是小写
    a = "ABCDE"
    print a.isupper()       # True, 所有字符都是大写
    a = "Abcde"
    print a.istitle()       # True, 所有单词都是首字母大写,像标题
    a = "\t"
    print a.isspace()       # True, 所有字符都是空白字符、\t、\n、\r
    arr = (1, 2.1, -3, -4.5, '123a', 'abc', 'aBC', 'Abc', 'ABC', '\t')
    for a in arr:
        print a,isNum(a)

def fact(n):
    if n==1:
        return 1
    return n * fact(n - 1)
#返回数的上入整数,如math.ceil(4.1) 返回 5
#返回数的下舍整数,如math.floor(4.9)返回 4
#工具类用来处理赋值函数
class DataHolder(object):
	def __init__(self, value=None):
		self.value = value
	def set(self, value):
		self.value = value
		return value
	def get(self):
		return self.value

if __name__=="__main__":
	data=DataHolder(None);  
	#argv 参数类型都为str
	for arg in sys.argv[1:]:
		if arg.isdigit():
			print 'int',arg
		elif data.set(isFloat(arg)) :
			print 'float',round(data.get(),1)
		else:
			print 'other',arg
	print fact(10) #递归大量调用函数堆栈 如果不够会报错
#下面2个也可以判断类型
#type(eval("123")) == int
#type(eval("123.23")) == float
#输入python test.py  10 1.55 '10'
#输出
#int 10
#float 1.6
#other other '10'
#3628800

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值