往期目录:
Python学习笔记(1)输入和输出
https://editor.csdn.net/md/?articleId=134531933
常用内置数学函数
1.求绝对值函数abs,用法:
a=abs(-9)
print(a) #函数返回其绝对值
2.将整数转换为二进制字符串bin函数:
print(bin(-9)) #结果-0b1001
print(bin(9)) #结果0b1001
3.返回整数对应的ASCLL函数的字符chr函数
print(chr(65)) #输出结果:A
4.返回商和余数divmod函数
print(divmod(9, 2)) #结果:(4, 2)
5.返回字符串中表达式中的值eval函数
a = 3
c = eval('a*a+1') #必须是有效的常量否则报错
print(c) #输出结果:10
6.四舍五入,保留指定位数的小位数round函数
a = 1.23456789
print(round(a,2)) #运行结果:1.23
print(round(a,3)) #运行结果:1.235
print(round(a,4)) #运行结果:1.2346
7.求和函数,sum
print(sum({1,2,3})) # 注意sum小括号里要有个大括号或者中括号,即它的作用对象是列表或元组
#运行结果:6
math模块函数
在使用这个模块函数时,需要在最前面加上import math来导入math模块
1.引用圆周率pi
import math
a = math.pi
print(a)
#运行结果:3.141592653589793
2.引入常数e
# 为了简洁之后省略import math
a = math.e
print(a)
# 运行结果:2.718281828459045
3.返回不小于x的最小整数
print(math.ceil(3.1)) #运行结果:4
print(math.ceil(3.9)) #运行结果:4
print(math.ceil(-3.1)) #运行结果:-3
4.返回非负数x的阶乘
print(math.factorial(0)) # 运行结果:1
print(math.factorial(3)) # 运行结果:6
print(math.factorial(-3))# 报错无法运行
5.返回x除以y的余数
# 和%的作用差不多,有趣的是他返回的总是浮点数
print(math.fmod(3, 2)) #运行结果:1.0
print(3 % 2) #运行结果:1
6.返回x和y的最大公约数
print(math.gcd(12, 8)) #运行结果:4
#注意:内部参数必须是整数否则报错
7.返回e的x次次方
print(math.exp(2))# 运行结果:7.38905609893065