• 技术文章 >常见问题 >Python常见问题

    如何用python求圆周率?

     Ly Ly2020-05-16 10:53:05原创4596

    在Python中如何计算圆周率呢?下面是详细的代码和运行结果:

    如下图是详细的计算圆周率的代码:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    import math

    import time

    scale = 10

    print("执行开始")

    t=time.process_time()

    for i in range(scale+1):

    a,b = '**'*i,'|..'*(scale-1)

    c = (i/scale)*100

    π = 4*(4*math.atan(1/5)-math.atan(1/239))

    print("%{:3}[{}->{}".format(a,b,c))

    time.sleep(0.1)

    print(π)

    print("{:.2f}s".format(t))

    print("执行结束")

    下图是代码运行结果:

    p1.jpg

    专题推荐:python
    上一篇:Python中数组和矩阵可以转换吗? 下一篇:初学者如何下载Python?

    相关文章推荐

    • python中圆周率怎么表示• python如何验证字符串是否符合正则表达式?• 如何用Python统计不同字符个数?• cmd中怎么调用python脚本?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网