聪明方法学python task2,task3

发布时间 2023-11-25 21:44:01作者: QG许诺

数据类型

  • 类型Type本身也是类型

  • Float默认双精度

  • Moudle

    内置常数

  • None表示空值

  • tau(2*pi)

  • inf,浮点正无穷大,等价于float(‘inf’),负无穷大使用-math.inf

    逻辑运算符

  • not类比c语言‘!’

  • and,or类比&&和||

    浮点数误差

  • `print(0.1 + 0.1 == 0.2) # True` 
    `print(0.1 + 0.1 + 0.1 == 0.3) # False!`
    `print(0.1 + 0.1 + 0.1) # 0.30000000000000004`
    `print((0.1 + 0.1 + 0.1) - 0.3) # 特别小,5.551115123125783e-17,不是0`

    isinstance

  • - import numbers 
     def isNumber(x):
    return isinstance(x, numbers.Number) # 可以应对任何类型的数字

  • isinstance()比type()更具有稳健性

    变量

  • 不能以下划线为开头

  • 不要与保留字(关键字)相同

    import keyword
    keyword.kwlist

    函数

  • 语句与表达式

    表达式本身是值,计算结果也是值;语句本身不是值,不能打印,但可以执行一些操作

  • 函数无返回语句时,函数会返回None

    一些基本数学函数

  • print(abs(-3))#绝对值函数
    3
    print(max(2,6))#返回最大值
    6
    print(min(6,3))
    3
    print(pow(2,10))#次方运算,等价于2**10
    1024
    print(round(2.38,1))#取最近的数,逗号后面的数是小数位数,并不完全是四舍五入
    2.4
    print(round(3.124,2))
    3.12