• python 练习题


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

    有关正态分布的问题:

     1 # -*- coding: cp936 -*-
     2 import math
     3 a=0
     4 u=0
     5 x=0
     6 while 1:
     7     a =int(input("please input a :"))
     8     ifa<0:
     9        print "请重新输入:"
    10     elifa>0:
    11        break;
    12 u = int(input("please intput u :"))
    13 x = int(input("please intput x :"))
    14 fs =(1/math.sqrt(2*math.pi)*a)*math.exp(0.5*(math.pow(((x-u)/a),2)))
    15 print "this fs is :%.2f"%fs;
    View Code

    关于平抛运动矢量位移:

     1 v0 =10
     2 g= 9.81
     3 #t = int(input("please input a time:"))
     4 #h = v0*t+0.5*g*t**2
     5 #print "this h is :%.2f"%h;
     6 
     7 while 1:
     8     h =int(input("please input a hight:"))
     9     #h =v0*t+0.5*g*t**2
    10     t1=(-v0+(v0**2-4*0.5*g*(-h))**0.5)/(2*0.5*g)
    11     t2=(-v0-(v0**2-4*0.5*g*(-h))**0.5)/(2*0.5*g)
    12     ift1>0:
    13        print "this time1 is:%.2f"%t1;   
    14     ift2>0:
    15        print "this time2 is :%.2f"%t2;
    View Code

    python的函数式编程

      判断一个数是不是质数:

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

      求最大公约数:

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

    计算给定数字的阶乘:

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

    python的reduce函数的应用:

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

    蒙特卡洛近似求解

      1.求圆周率:

     1 import random
     2 import math
     3 def round_(num):
     4     i=0
     5    number=0.0
     6     whilei<num:
     7        x = random.random()
     8        y = random.random()
     9       
    10        y_=s(x)
    11        if y<=y_:
    12            number+=1
    13        i+=1
    14     return4*number/num
    15 def s(a):
    16     returnmath.sqrt(1-math.pow(a,2))
    View Code

      2.求面积:

     1 import random
     2 def f(num):
     3     i=0
     4     s=0.0
     5     while i<num:
     6         x = random.random()
     7         #print x
     8         y = random.random()
     9         y_=ft(x)
    10         if y<=y_:
    11             s+=1
    12         i+=1
    13     return s/num
    14   
    15    
    16 def ft(a) :
    17     return a*a
    View Code
  • 相关阅读:
    Visual Studio 编译使用 Trilinos中 Belos库 求解线性方程组 (待续)
    Visual Studio编译使用CLAPACK
    Visual Studio 页面文件太小,无法完成操作
    wpf中的BringToFront
    想买的书
    远程桌面使用matlab报错License Manager Error -103的解决办法
    工厂模式-理解Spring的Bean工厂(马士兵经典例子)
    面向对象、继承封装、多态小例子(马士兵)
    检查pod版本及更新pod
    mac之开发中的环境配置
  • 原文地址:https://www.cnblogs.com/wq920/p/3270020.html
Copyright © 2020-2023  润新知