Python_day3_random和math模块的使用

Python_day3_random和math模块的使用

1. math模块

math模块:提供一系列关于数学运算的方法

import math			#使用math模块前先调用
a=dir(math)			#查看对应模块中的方法,类,属性,变量等各种信息
print(a)

#结果显示
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 
'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 
'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 
'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 
'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 
'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf',
 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh',
  'tau', 'trunc']
#常见的几种math模块的使用
import math
a=math.ceil(3.3)		#向上取整4
b=math.floor(2.9)		#向下取整2
c=math.pi				#PI的值(3.1415926...)
d=math.e				#自然常数(2.718281828459045)
e=math.pow(2,3)     	#2的3次方(2**3) 
f=math.sqrt(4)   		#开根号

print(a,b,c,d,e,f)
#结果显示
4 2 3.141592653589793 2.718281828459045 8.0  2.0

#若不知道math模块的用法可使用help
help(math.pow#查看方法的帮助文档

2.random模块(随机数模块)

#常见的几种随机数模块
import random

a=random.random()		#获取一个[0,1) 的随机数
b=random.randint(x,y)	#表示产生一个[x,y]之间的随机数,注意是闭区间
发布了20 篇原创文章 · 获赞 3 · 访问量 864

猜你喜欢

转载自blog.csdn.net/weixin_44244493/article/details/103530935