Python-math模块的知识点

Python-math模块的知识点总结


# 1.import math   导入math 模块,引用函数名时前加  math
# 计算半径为 5 的圆的面积
import math
radius = 5
area = math.pi * radius ** 2
print('{:.3f}'.format(area))
print('%.3f' % area)

# 2.from math import *    # 导入 math 模块中的所有函数,引用时直接引用函数名
# * 是通配符
# 计算半径为 5 的圆的面积,小数点三位
from math import *
radius = 5
area = pi * radius ** 2
print(area)


# 1.math.fabs(x): 以浮点数形式返回  x  的绝对值
import math
print(math.fabs(-6))


# 2.math.factorial(x)  计算阶乘  x 为非负整数
import math
print(math.factorial(5))


# 3.math.fusm():返回浮点数迭代求和的精确值,避免多次求和导致精度损失
import math
print(sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]))
print(math.fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]))
print(0.1)
print(.1)
print(sum([1, 1]))    # sum 求和

# 4.math.gcd( * integers): 返回给定整数参数的 最大公约数,参数可以有很多
import math
print(math.gcd(88, 44, 22))    # 注意解释器的版本
print(math.gcd(0, 0))
print(math.gcd(0, 0))

# 5.math.lcm( * integers): 返回给定整数参数的 最小公约数,参数可以有很多
import math
print(math.lcm(88, 44, 22))
print(math.lcm(0, 88))    # 0
print(math.lcm())    # 0


# 6.math.prod(iterable, * ,start = 1 )    计算输入的可迭代对象 iterable 中 所有元素的积
import math
print(math.prod([1, 2, 4]))
print(math.prod([1, 2, 4], start=2))


# 7.math.floor(x): 返回不大于 x 的最大整数
import math
print(math.floor(9.5))


# 8.math.ceil(x): 返回不小于 x 的最小整数
import math
print(math.ceil(9.5))


# 9.math.exp(x):  返回 e 的 x  次方
import math
print(math.exp(2))


# 10.math.pow(x, y):返回 x 的 y 次 幂 ,结果为浮点数
import math
print(math.pow(2, 3))


# 11.math.sqrt(x): 返回 x 的平方根
import math
print(math.sqrt(100))     


# 12.math.isqrt(n):返回非负整数 n 的整数平方根    向下取整 :   相当于 a 的 2 次方 <=  n   的最大整数 a
import math
print(math.isqrt(99))      


# 13 math.log2(x):返回以 2 为底 的 x 的对数,这种方法比log(x, 2) 的值更加 精准
import math
print(math.log2(4))


# 14 math.log10(x):返回以 10 为底 的 x 的对数,这种方法比log(x, 10) 的值更加 精准
import math
print(math.log10(100))      # ****** 刘云说  10


# 15.math.cos(x): 返回 x 的余弦函数,x 为弧度
import math
print(math.cos(math.pi / 3))    # 刘云说 math.pi 是 -6


# 16.math.sin(x): 返回 x 的正弦函数,x 为弧度
import math
print(math.sin(math.pi / 3))


# 17.math.hypot(x, y): 返回坐标 (x, y) 到 原点(0, 0) 坐标之间的距离
import math
print(math.hypot(3, 4))      # 刘云说 5


# 18.math.dist(p, q): 返回 p, q 两点之间的欧几里得距离,以一个坐标序列或可迭代对象的形式给出    # 了解,不需要会


# 19.math.degrees(x): 弧度值转角度值
import math
print(math.degrees(math.pi / 3))


# 20.math.radians(x):角度值转弧度值
import math
print(math.radians(90))


# 21.math.pi: 返回圆周率常数的值
import math
print(math.pi)


# 22.math.e : 返回自然常数 e 的值
import math
print(math.e)


# 23.math.comb(n, k): 返回不重复且无顺序地从 n 项中选择 k 项 的方法总数    
import math
print(math.comb(6, 3))
# 1.import math   导入math 模块,引用函数名时前加  math
# 计算半径为 5 的圆的面积
# import math
# radius = 5
# area = math.pi * radius ** 2
# print('{:.3f}'.format(area))
# print('%.3f' % area)

# 2.from math import *    # 导入 math 模块中的所有函数,引用时直接引用函数名
# * 是通配符
# 计算半径为 5 的圆的面积,小数点三位
# from math import *
# radius = 5
# area = pi * radius ** 2
# print(area)


# 1.math.fabs(x): 以浮点数形式返回  x  的绝对值
# import math
# print(math.fabs(-6))


# 2.math.factorial(x)  计算阶乘  x 为非负整数
# import math
# print(math.factorial(5))


# 3.math.fusm():返回浮点数迭代求和的精确值,避免多次求和导致精度损失
# import math
# print(sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]))
# print(math.fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]))
# print(0.1)
# print(.1)
# print(sum([1, 1]))    # sum 求和

# 4.math.gcd( * integers): 返回给定整数参数的 最大公约数,参数可以有很多
# import math
# print(math.gcd(88, 44, 22))    # 注意解释器的版本3.8
# print(math.gcd(0, 0))
# print(math.gcd(0, 0))

# 5.math.lcm( * integers): 返回给定整数参数的 最小公约数,参数可以有很多
# import math
# print(math.lcm(88, 44, 22))
# print(math.lcm(0, 88))    # 0
# print(math.lcm())    # 0


# 6.math.prod(iterable, * ,start = 1 )    计算输入的可迭代对象 iterable 中 所有元素的积
# import math
# print(math.prod([1, 2, 4]))
# print(math.prod([1, 2, 4], start=2))


# 7.math.floor(x): 返回不大于 x 的最大整数
# import math
# print(math.floor(9.5))


# 8.math.ceil(x): 返回不小于 x 的最小整数
# import math
# print(math.ceil(9.5))


# 9.math.exp(x):  返回 e 的 x  次方
# import math
# print(math.exp(2))


# 10.math.pow(x, y):返回 x 的 y 次 幂 ,结果为浮点数
# import math
# print(math.pow(2, 3))


# 11.math.sqrt(x): 返回 x 的平方根
# import math
# print(math.sqrt(100))     

# 12.math.isqrt(n):返回非负整数 n 的整数平方根    向下取整 :   相当于 a 的 2 次方 <=  n   的最大整数 a
# import math
# print(math.isqrt(99))     


# 13 math.log2(x):返回以 2 为底 的 x 的对数,这种方法比log(x, 2) 的值更加 精准
# import math
# print(math.log2(4))


# 14 math.log10(x):返回以 10 为底 的 x 的对数,这种方法比log(x, 10) 的值更加 精准
# import math
# print(math.log10(100))     


# 15.math.cos(x): 返回 x 的余弦函数,x 为弧度
# import math
# print(math.cos(math.pi / 3))    


# 16.math.sin(x): 返回 x 的正弦函数,x 为弧度
# import math
# print(math.sin(math.pi / 3))


# 17.math.hypot(x, y): 返回坐标 (x, y) 到 原点(0, 0) 坐标之间的距离
# import math
# print(math.hypot(3, 4))      


# 18.math.dist(p, q): 返回 p, q 两点之间的欧几里得距离,以一个坐标序列或可迭代对象的形式给出    # 了解,不需要会


# 19.math.degrees(x): 弧度值转角度值
# import math
# print(math.degrees(math.pi / 3))


# 20.math.radians(x):角度值转弧度值
# import math
# print(math.radians(90))


# 21.math.pi: 返回圆周率常数的值
# import math
# print(math.pi)


# 22.math.e : 返回自然常数 e 的值
# import math
# print(math.e)


# 23.math.comb(n, k): 返回不重复且无顺序地从 n 项中选择 k 项 的方法总数     # 了解一下
# import math
# print(math.comb(6, 3))
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值