Python零基础学习笔记(八)—— 数学功能

本文深入讲解了Python中常用的数学函数,包括绝对值、比较运算、最大值、最小值、幂运算、四舍五入、向上取整、向下取整、开方等操作,并通过实例演示了如何使用这些函数进行数值计算。
一定要导入math包,不然后面会报错
今日学习部分及执行结果如下图
import math
# abs() 返回数的绝对值
a1 = -10
a2 = abs(a1)
print(a2)

#比较两个数的大小
a3 = 10
a4 = 9
print((a3>a4)-(a3<a4))

# max() 返回参数中的最大值
print(max(1,2,3,4,5))
# min() 返回参数中的最小值
print(min(1,2,3,4,5))

# pow() 求x的y次方 例如2^4
print(pow(2,4))

#round(x[, n])  x为数值,n为保留位数,n可有可无
# 四舍五入
print(round(3.576))
print(round(3.492))
#四舍五入保留位数
print(round(3.576, 1))
print(round(3.492, 2))

# math.ceil() 向上取整
print(math.ceil(8.1))
print(math.ceil(5.9))

# math.floor() 向下取整
print(math.floor(8.1))
print(math.floor(5.9))

# math.modf() 返回整数部分与小数部分
print(math.modf(12.3))

# math.sqrt() 开方
print(math.sqrt(25))
6f8a4caf03d58fc8919b7d6a40970c92e978d446

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值