常用的数学函数库math
import math
print(math.pi)
print(math.sin(math.pi/4))
输出:
3.141592653589793
0.7071067811865476
格式化字符串
applePrice = 5
appleWeight = 2
appleCost = applePrice * appleWeight
print(applePrice,appleWeight,appleCost)
输出结果:
5 2 10
但是不明白上面的数字的意思,可以输出字符串来实现:
print("苹果的价格:{} 苹果的重量{} 总的价格{}".format(applePrice,appleWeight,appleCost))
输出结果:
苹果的价格:5 苹果的重量2 总的价格10
通过{}来实现字符串的格式化
两个数值之间的交换
a=10
b=20
print("交换前:a={} b={}".format(a,b))
a,b=b,a
print("交换后:a={} b={}".format(a,b))
交换前:a=10 b=20
交换后:a=20 b=10
保留小数点后几位 round
print("保留小数点后6位:{}".format(round(math.pi,10)))
保留小数点后6位:3.1415926536
Math常用函数
#乘方
print(math.pow(3,9))
print(3**9)
#向下取整
print(math.floor(3.1415926))
#向上取整
print(math.ceil(3.1415926))
#角度装弧度
print(math.radians(90))
19683.0
19683
3
4
1.5707963267948966
#取最小值
print("这组数中的最小值:{}".format(min(10,23,1,-1)))
#取最小大值
print("这组数中的最大值:{}".format(max(10,23,1,-1)))
#求商和余数
print("10÷3的商和余数分别是:{}".format(divmod(10,3)))
这组数中的最小值:-1
这组数中的最大值:23
10÷3的商和余数分别是:(3, 1)
bool值:注意True 和False首字母大写
print(True and True)
print(True and False)
print(not True)
True
False
False