python 练习题

  python是一个功能很强大的语言,他可以解决各种在数学问题,下面我分享一些练习题供大家参考:

有关正态分布的问题:

 # -*- coding: cp936 -*-
import math
a=0
u=0
x=0
while 1:
a =int(input("please input a :"))
ifa<0:
print "请重新输入:"
elifa>0:
break;
u = int(input("please intput u :"))
x = int(input("please intput x :"))
fs =(1/math.sqrt(2*math.pi)*a)*math.exp(0.5*(math.pow(((x-u)/a),2)))
print "this fs is :%.2f"%fs;

关于平抛运动矢量位移:

 v0 =10
g= 9.81
#t = int(input("please input a time:"))
#h = v0*t+0.5*g*t**2
#print "this h is :%.2f"%h; while 1:
h =int(input("please input a hight:"))
#h =v0*t+0.5*g*t**2
t1=(-v0+(v0**2-4*0.5*g*(-h))**0.5)/(2*0.5*g)
t2=(-v0-(v0**2-4*0.5*g*(-h))**0.5)/(2*0.5*g)
ift1>0:
print "this time1 is:%.2f"%t1;
ift2>0:
print "this time2 is :%.2f"%t2;

python的函数式编程

  判断一个数是不是质数:

  >>>f = lambda x:not reduce(lambda x,y:x or y,[x%i==0 for i inrange(2,x)])
>>> f(97)
True
>>> f(96)
False

  求最大公约数:

 >>> f = lambdam,n:[i for i in range(min(m,n),0,-1) if m%i==0 and n%i==0][0]
>>> f(123,77)
1
>>> f(125,100)
25

计算给定数字的阶乘:

 >>> f = lambdax:reduce(lambda x,y:x*y,[i for i in range(1,x+1)])
>>> f(5)
120
 >>> zip3 = lambdal1,l2,l3:[(l1[i],l2[i],l3[i]) for i in range(len(l1))]
>>>zip3([1,2,3],['a','b','c'],[1j,2j,3j])
[(1, 'a', 1j), (2, 'b', 2j), (3, 'c', 3j)]

python的reduce函数的应用:

 >>> f = lambdal:reduce(lambda x,y:x*y,l)
>>> f(l)
0
>>>f([1,2,2,3,4])
48

蒙特卡洛近似求解

  1.求圆周率:

 import random
import math
def round_(num):
i=0
number=0.0
whilei<num:
x = random.random()
y = random.random() y_=s(x)
if y<=y_:
number+=1
i+=1
return4*number/num
def s(a):
returnmath.sqrt(1-math.pow(a,2))

  2.求面积:

 import random
def f(num):
i=0
s=0.0
while i<num:
x = random.random()
#print x
y = random.random()
y_=ft(x)
if y<=y_:
s+=1
i+=1
return s/num def ft(a) :
return a*a
上一篇:A1486. 树(王康宁)


下一篇:好用的 diskpart 命令,操作磁盘分区,并创建 EFI 引导分区