def facto(n):
if(n==0): return 1
s=1
for i in range(1,n+1):
s *= i
return s
for i in range(11):
s=facto(i)
print i,s
0 1 1 1 2 2 3 6 4 24 5 120 6 720 7 5040 8 40320 9 362880 10 3628800

import math
def facto(n):
if(n==0): return 1
s=1
for i in range(1,n+1):
s *= i
return s
N=5
sum=0.
x=math.pi/4.
for n in range(N+1):
sum += (-1.)**n/facto(2*n+1)*x**(2*n+1)
print n,sum
print x,math.sin(x),sum
0 0.785398163397 1 0.704652651209 2 0.707143045779 3 0.707106469575 4 0.707106782937 5 0.70710678118 0.785398163397 0.707106781187 0.70710678118
###### 呼叫數學函數模組及其應用
import math #呼叫數學函數模組
pi=math.pi #圓周率
print 'pi=',pi
x=pi/3.
deg=math.degrees(x) #將弧度轉為角度
print 'x=',x,' degree of x=',deg
print 'sin(x)=',math.sin(x) #三角函數
print 'cos(x)=',math.cos(x)
print 'tan(x)=',math.tan(x)
deg2=45.
rad2=math.radians(deg2) #將角度轉為弧度
print 'deg2=',deg2,' rad2=',rad2
y=math.atan(rad2) #反三角函數
print 'atan(rad2)=',y
x=1.
print 'x=',x,' 自然指數exp(x)=',math.exp(x),' exp(2)=',math.exp(2.)
z=10.
print 'z=',z,' 自然對數log(z)=',math.log(10.)
print '常用對數log10(z)=',math.log10(10.),' log10(12345.)=',math.log10(12345.)
print '開平方根math.sqrt(100.)=',math.sqrt(100.),'math.sqrt(2.)=',math.sqrt(2.)
x=-10.
print '絕對值:x,math.fabs(x)=',x,math.fabs(x)
pi= 3.14159265359 x= 1.0471975512 degree of x= 60.0 sin(x)= 0.866025403784 cos(x)= 0.5 tan(x)= 1.73205080757 deg2= 45.0 rad2= 0.785398163397 atan(rad2)= 0.665773750028 x= 1.0 自然指數exp(x)= 2.71828182846 exp(2)= 7.38905609893 z= 10.0 自然對數log(z)= 2.30258509299 常用對數log10(z)= 1.0 log10(12345.)= 4.09149109427 開平方根math.sqrt(100.)= 10.0 math.sqrt(2.)= 1.41421356237 絕對值:x,math.fabs(x)= -10.0 10.0
import random
random.seed(1234)
for i in range(10):
r=random.random()
print ('%8d %8.3f %3d' %(i,r,int(r*10)))
0 0.966 9
1 0.441 4
2 0.007 0
3 0.911 9
4 0.939 9
5 0.582 5
6 0.672 6
7 0.084 0
8 0.766 7
9 0.237 2