python入门:有关math包以及内置函数的数值操作代码总结

# # 作者:王杰安 暨南大学 
# 5 numeirical value
def report_code(oper):
    result = eval(oper)
    print('{} = {}'.format(oper, result))

# data type
print('数值类型')
x = 123
y = 44.67
print('[{}] is [{}]'.format(x, type(x)))
print('[{}] is [{}]'.format(y, type(y)))

# built-in function
print()
print('内置数值操作')
x = 10
y = 3
print('{} = {}'.format('x+y',x+y))
print('{} = {}'.format('x-y',x-y))
print('{} = {}'.format('x*y',x*y))
print('{} = {}'.format('x/y',x/y))
print('{} = {}'.format('x//y',x//y))
print('{} = {}'.format('x%y',x%y))
#==================================================#
print()
print('复合赋值运算符')
print('原来的x是{}'.format(x), end=',')
x+=y
print('现在的x是{}'.format(x))
#==================================================#
print()
print('内置数值函数')
x = 10.5; y = 3; z = 2; m = -110
print('x = {}, y = {}, z = {}; m = {}'.format(x, y, z, m))
print('{} = {}'.format('abs(m)', abs(m)))
print('{} = {}'.format('divmod(x, y)', divmod(x, y)))
print('{} = {}'.format('pow(x, y)', pow(x, y)))
print('{} = {} # 均为整数'.format('pow(m, y, z)', pow(m, y, z)))
print('{} = {}'.format('round(x)', round(x)))
print('{} = {}'.format('round(x, 1)', round(x, 1)))
print('{} = {}'.format('max(x, y, z)', max(x, y, z)))
print('{} = {}'.format('min(x, y, z)', min(x, y, z)))
#==================================================#
print()
print('使用math库')
import math
print('[pi] : {}'.format(math.pi))
print('[e]  : {}'.format(math.e))
print('[inf]: {}'.format(math.inf))
print('[nan]: {}'.format(math.nan))
print()
print('数值函数')
x = -10.5; y = 3; z = 4; m = 48; n = 16
print('x = {}, y = {}, z = {}; m = {}; n = {}'.format(x, y, z, m, n))
fabs  = 'math.fabs(x)'
fmod  = 'math.fmod(x, y)'
fsum  = 'math.fsum([x, y, z])'
gcd   = 'math.gcd(m, n)'
trunc = 'math.trunc(x)'
modf  = 'math.modf(x)'
ceil  = 'math.ceil(x)'
floor = 'math.floor(x)'
facto = 'math.factorial(z)'
print('{} = {}'.format(fabs, eval(fabs)))
print('{} = {}'.format(fmod, eval(fmod)))
print('{} = {}'.format(fsum, eval(fsum)))
print('{} = {}'.format(gcd, eval(gcd)))
print('{} = {}'.format(trunc, eval(trunc)))
print('{} = {}'.format(modf, eval(modf)))
print('{} = {}'.format(ceil, eval(ceil)))
print('{} = {}'.format(floor, eval(floor)))
print('{} = {}'.format(facto, eval(facto)))
print()
print('数值函数')
report_code('math.pow(10,2)')
report_code('math.exp(2)')
report_code('math.sqrt(99)')
report_code('math.log(math.e**2)')
report_code('math.log(30, 10)')
report_code('math.log(10)')
report_code('math.log2(10)')
report_code('math.log10(100)')
report_code('math.degrees(math.pi)')
report_code('math.radians(180)')
report_code('math.hypot(2,3)')
print()
print('数值函数')
report_code('math.sin(math.pi)') # math.pi是一个近似数,所以返回值是接近0,而不为0
report_code('round(math.sin(math.pi), 10)') # 考虑四舍五入一下
report_code('math.cos(math.pi)')
report_code('math.tan(math.pi)')
report_code('math.asin(1)')
report_code('math.acos(1)')
report_code('math.atan(1)')

输出结果

数值类型
[123] is [<class 'int'>]
[44.67] is [<class 'float'>]

内置数值操作
x+y = 13
x-y = 7
x*y = 30
x/y = 3.3333333333333335
x//y = 3
x%y = 1

复合赋值运算符
原来的x是10,现在的x是13

内置数值函数
x = 10.5, y = 3, z = 2; m = -110
abs(m) = 110
divmod(x, y) = (3.0, 1.5)
pow(x, y) = 1157.625
pow(m, y, z) = 0 # 均为整数
round(x) = 10
round(x, 1) = 10.5
max(x, y, z) = 10.5
min(x, y, z) = 2

使用math库
[pi] : 3.141592653589793
[e]  : 2.718281828459045
[inf]: inf
[nan]: nan

数值函数
x = -10.5, y = 3, z = 4; m = 48; n = 16
math.fabs(x) = 10.5
math.fmod(x, y) = -1.5
math.fsum([x, y, z]) = -3.5
math.gcd(m, n) = 16
math.trunc(x) = -10
math.modf(x) = (-0.5, -10.0)
math.ceil(x) = -10
math.floor(x) = -11
math.factorial(z) = 24

数值函数
math.pow(10,2) = 100.0
math.exp(2) = 7.38905609893065
math.sqrt(99) = 9.9498743710662
math.log(math.e**2) = 2.0
math.log(30, 10) = 1.4771212547196624
math.log(10) = 2.302585092994046
math.log2(10) = 3.321928094887362
math.log10(100) = 2.0
math.degrees(math.pi) = 180.0
math.radians(180) = 3.141592653589793
math.hypot(2,3) = 3.605551275463989

数值函数
math.sin(math.pi) = 1.2246467991473532e-16
round(math.sin(math.pi), 10) = 0.0
math.cos(math.pi) = -1.0
math.tan(math.pi) = -1.2246467991473532e-16
math.asin(1) = 1.5707963267948966
math.acos(1) = 0.0
math.atan(1) = 0.7853981633974483

好物分享
python: 数据科学代码速查表(强烈推荐!)
入门总结:
python入门:有关字符串的操作代码总结
python入门:有关math包以及内置函数的数值操作代码总结
Python练习:
python:第二章 字符串和数值程序作业
python:第三章 程序流程控制作业
python:第三章 程序流程控制作业2
python:第四章 列表与元组作业
python:第四章 列表与元组作业2

发布了20 篇原创文章 · 获赞 3 · 访问量 1499

猜你喜欢

转载自blog.csdn.net/qq_42830966/article/details/104818029