【Python入门】:标准库math用法

#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Time   : 2021/11/4 11:48
# @Author : SandQuant

import math

print(math.e)  # 自然数e
print(math.pi)  # 自然数pi
print(math.inf)  # 无穷
print(math.nan)  # 缺失

print(math.fabs(-100))  # 返回绝对值
print(math.fmod(10.5, 3))  # 取余数
print(math.fsum([1, 2, 3]))  # 求和
print(math.gcd(24, 30))  # 最大公因数
print(math.trunc(-66.5))  # 最接近0的整数
print(math.modf(-7.5))  # 小数部分 和 整数部分
print(math.ceil(9.5))  # 向上取整
print(math.floor(9.5))  # 向下取整
print(math.factorial(4))  # 阶乘

# 幂运算
print(math.pow(10, 2))  # 幂运算
print(math.exp(2))  # e为底幂运算
print(math.sqrt(99))  # 开2次方

# 对数运算
print(math.log(math.e**2))  # 对数运算,默认底为e
print(math.log(30, 10))  # 指定底为10
print(math.log2(64))  # 底为2
print(math.log10(100))  # 底为10

# 三角运算
print(math.degrees(math.pi))  # 弧度 转 角度
print(math.radians(180))  # 角度 转 弧度
print(math.hypot(3, 4))  # 勾股结果

print(math.sin(math.pi))  # math.pi是一个近似数,所以返回值是接近0,而不为0
print(round(math.sin(math.pi)))  # 四舍五入才为0
print(math.cos(math.pi))  # 余弦
print(math.tan(math.pi))  # 正切
print(math.asin(1))  # 反正弦
print(math.acos(1))  # 反余弦
print(math.atan(1))  # 反正切



欢迎关注~ SandQuant 专注于全球金融数据和量化投资策略

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值