前言
本章将会讲解Python编程中的 数值类型 数字计算
一.Python数值类型
1.数学计算(熟悉)
对于数学计算,除了前面提到过的简单的加减乘除等等,更多的科学计算需要 导入 math 这个标准库(不需要安装,但是要导入),它包含了绝大多数我们可能需要的科学计算函数。
数学计算函数
math.ceil(x)
math.floor(x)
math.pow(x)
2.导入方法:
1. #导入标准库 math
2. import math
向上取整ath.ceil(x)
"""
Return the ceiling of x as an Integral.
This is the smallest integer >= x.
"""
#这是一个最小的整数,还要大于等于X
import math #向上取整 得5
print(math.ceil(4.1)) #向上取整
向下取整math.floor(x)
"""
Return the floor of x as an Integral.
This is the largest integer <= x.
"""
#这是一个最大的整数还小于等于X
import math
print(math.floor(4.5)) #向下取整 得4
x**y 返回次幂math.pow(x)
""" Return x**y (x to the power of y). """
返回次幂
import math
print(math.pow(2,3)) # x**y 2**3
Python内置数学计算(直接使用)
- abs(x)
- round(x)
""" Return the absolute value of the argument. """
这个参数的绝对值
a=-10
print(abs(a)) #打印取绝对值后的值
四舍五入 round(x)
# 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