Python入门第2课——数学模块初步(只读课堂)

    科技2022-07-15  122

    In [1]: #欢迎来到“只读课堂”!

     

    In [2]: #同学们,python这门语言不仅可以做大数据,还可以做计算器哦!下面,我今天来讲讲python计算器的运用。

     

    In [3]: #首先,要在程序中设置数学模块,不然不能用

     

    In [4]: math.pi

    ---------------------------------------------------------------------------

    NameError Traceback (most recent call last)

    <ipython-input-4-9ee7c101b273> in <module>()

    ----> 1 math.pi

     

    NameError: name 'math' is not defined

     

    In [5]: #上面就说不可用了吧!正确用法:

     

    In [6]: import math

     

    In [7]: #先最简单的:加减乘除

     

    In [8]: 5+8

    Out[8]: 13

     

    In [9]: 6-3

    Out[9]: 3

     

    In [10]: 4*9

    Out[10]: 36

     

    In [11]: 30/10

    Out[11]: 3.0

     

    In [12]: #顺便说一句,python新版本(3.0)比之前的旧版本(2.5)在这里多一个内容,就是在除法运算的时候它会有加一个“.0”,不过这个暂时没多大影响。

     

    In [13]: #数学模块内容:+ 加法

        ...: #- 减法

        ...: #* 乘法

        ...: #/ 除法

        ...: #**乘方

        ...: #% 取余数

        ...: #import math 数学模块,有大量函数供使用

        ...: #math.pi 圆周率

        ...: #math.sin 算度数

        ...: #math.floor 向下取整数

        ...: #math.seil 向上取整数

        ...: 2**2

    Out[13]: 4

     

    In [14]: #这便是乘方

     

    In [15]: math.floor(87)

    Out[15]: 87

    In [16]: #向下取整数

     

    In [17]: math.seil(27.65)

    ---------------------------------------------------------------------------

    AttributeError Traceback (most recent call last)

    <ipython-input-17-2044eceb7fa8> in <module>()

    ----> 1 math.seil(27.65)

     

    AttributeError: module 'math' has no attribute 'seil'

     

    In [18]: #现在又不可用了

     

    In [19]: import math

     

    In [20]: 50+10-10*6/2

    Out[20]: 30.0

     

    In [21]: #混合运算

     

    In [22]: 你*5

    ---------------------------------------------------------------------------

    NameError Traceback (most recent call last)

    <ipython-input-22-123d15235c15> in <module>()

    ----> 1 你*5

     

    NameError: name '你' is not defined

     

    In [23]: #当然,汉字就不行了!

     

    In [24]: print("\n本次只读课堂的python教程就到这了,欢迎下一次的收看!\n")

     

    本次只读课堂的python教程就到这了,欢迎下一次的收看!

    Processed: 0.012, SQL: 8