目录
本章将会讲解Python编程中的 数值类型 数字计算
对于数学计算,除了前面提到过的简单的加减乘除等等,更多的科学计算需要 导入 math 这个标准库(不需要安装,但是要导入),它包含了绝大多数我们可能需要的科学计算函数。
数学计算函数
#导入标准库 math
import math
"""
Return the ceiling of x as an Integral.
This is the smallest integer >= x.
"""
#这是一个最小的整数,还要大于等于X
import math #向上取整 得5
print(math.ceil(4.1)) #向上取整
"""
Return the floor of x as an Integral.
This is the largest integer <= x.
"""
#这是一个最大的整数还小于等于X
import math
print(math.floor(4.5)) #向下取整 得4
""" Return x**y (x to the power of y). """
返回次幂
import math
print(math.pow(2,3)) # x**y 2**3
""" Return the absolute value of the argument. """
这个参数的绝对值
a=-10
print(abs(a)) #打印取绝对值后的值
# Python3 进行改良 不再是四舍五入 而是 四舍六入五成偶
print(round(4.1)) #舍去为4
print(round(4.5)) #舍去为4(Python2和Python3区别 2 中会进入为5 3 中不会)
print(round(4.6)) #进1为5
print(round(3.5)) #4
#vars([object]) -> dictionary 保留小数
print(round(4.5, 1)) #得 4.5
创作不易,求关注,点赞,收藏,谢谢~