#-*-coding:utf-8-*-
'''
Created on 2016年3月20日
@author: Zroad
'''
"""
一、常见数学常量:pi,e等
"""
import math
print "math.pi=",math.pi
print "math.e=",math.e
"""
二、常用数学函数
"""
print "math.sin(pi)=",math.sin(math.pi)
print "math.tan(pi)=",math.tan(math.pi)
print "math.sqrt(4)=",math.sqrt(4) #2.0
#不在math模块下的内置数学函数
print "pow(2,5)=",pow(2,5) #32.0
print "abs(-45)=",abs(-45),abs(-0.09) #45,0.09
"""
三、sum函数:
1、sum(...)
sum(sequence[, start]) -> value
Return the sum of a sequence of numbers (NOT strings) plus the value
of parameter 'start' (which defaults to 0). When the sequence is
empty, return start.
2、sum函数中传入的参数序列不能嵌套
"""
print sum((1,2,3,4)) #10
print sum([1,2,3,4]) #10
print sum([2,4,6,8],3) #23
print sum([2,4,6,8],1000) #1020
#print sum([2,4,6,8,[10,20]])
"""
四、求最大、最小值:
min(...)
min(iterable[, key=func]) -> value
min(a, b, c, ...[, key=func]) -> value
With a single iterable argument, return its smallest item.
With two or more arguments, return the smallest argument.
"""
print max([1,2,3,4]) #4
print max(1,2,3,4,5) #5
print max([1,2,3,4,[5,6]]) #[5,6]
print max([1,2,3,4,[0,5]]) #[0,5]
print max([1,2,3,4,5,[0,0]]) #[0,0]
print max([1,2,3,4,5,{6:7}]) #{6: 7}
print "--------------------"
print min([1,2,3,4]) #1
print min(1,2,3,4,5) #1
print min([1,2,3,4,[5,6]]) #1
print min([1,2,3,4,[0,5]]) #1
print min([1,2,3,4,5,[0,0]]) #[0,0]
print min([1,2,3,4,5,{6:7}]) #1
"""
五、round函数:提供数据的四舍五入方法
"""
print round(2.25) #2.0
print round(2.75) #3.0
print round(-2.25) #-2.0
print round(-2.75) #-3.0
print round(3.14159399482,2) #3.14
print round(3.14159399482,3) #3.142
print round(3.14159399482,4) #3.1416
print round(3.14159399482,5) #3.14159
print round(-3.14159399482,2) #-3.14
print round(-3.14159399482,3) #-3.142
print round(-3.14159399482,4) #-3.1416
print round(-3.14159399482,5) #-3.14159
python常用数学函数
最新推荐文章于 2022-10-28 22:16:25 发布