python如何导入math模块和time模块_python模块2 math\random\re\time\datetime模块

知识内容:

1.math模块

2.random模块

3.re模块

4.time模块

5.datetime模块

一、math模块

1.math模块的作用:  它提供了一些基本的数学运算的函数,比如说对数、三角函数、开发、乘方等

2.math模块中的内容

1 >>> importmath2 >>>dir(math)3 [‘__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‘]

3.math模块的主要方法

(1)常量(e和π)

1 e = 2.718281828459045

2 pi = 3.141592653589793

返回常数:

importmathprint(math.pi)print(math.e)

(2)求对数

log(x, a)  # 如果不写a默认为e

log10(x)

1 >>> importmath2 >>> math.log(100, 10)3 2

4 >>> math.log(10, 100)5 0.5

6 >>> math.log10(100)7 2.0

8 >>> math.log10(1000)9 3.0

(3)普通计算

modf(x): 返回x的小数与整数部分

pow(x, y):  计算x**y

sqrt(x): 开方计算

1 >>> importmath2 >>> math.modf(5.23)3 (0.23000000000000043, 5.0)4 >>> math.modf(5.31)5 (0.3099999999999996, 5.0)6 >>> math.pow(2, 3)7 8.0

8 >>> math.pow(2, 2)9 4.0

10 >>> math.sqrt(9)11 3.0

12 >>> math.sqrt(4)13 2.0

二、random模块

1.random模块的作用:  生成随机数

2.random模块中的内容

1 >>> importrandom2 >>>dir(random)3 [‘BPF‘, ‘LOG4‘, ‘NV_MAGICCONST‘, ‘RECIP_BPF‘, ‘Random‘, ‘SG_MAGICCONST‘, ‘SystemRandom‘, ‘TWOPI‘, ‘_BuiltinMethodType‘, ‘_MethodType‘, ‘_Sequence‘, ‘_Set‘, ‘__all__‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘_acos‘, ‘_bisect‘, ‘_ceil‘, ‘_cos‘, ‘_e‘, ‘_exp‘, ‘_inst‘, ‘_itertools‘, ‘_log‘, ‘_pi‘, ‘_random‘, ‘_sha512‘, ‘_sin‘, ‘_sqrt‘, ‘_test‘, ‘_test_generator‘, ‘_urandom‘, ‘_warn‘, ‘betavariate‘, ‘choice‘, ‘choices‘, ‘expovariate‘, ‘gammavariate‘, ‘gauss‘, ‘getrandbits‘, ‘getstate‘, ‘lognormvariate‘, ‘normalvariate‘, ‘paretovariate‘, ‘randint‘, ‘random‘, ‘randrange‘, ‘sample‘, ‘seed‘, ‘setstate‘, ‘shuffle‘, ‘triangular‘, ‘uniform‘, ‘vonmisesvariate‘, ‘weibullvariate‘]

3.random模块的主要方法

choice(): 用于从序列中任意选择一个元素的函数

getrandbits():  生成指定二进制位数的随机整数

randrange():  生成指定范围内(包含左边界不包含右边界)随机数(整数)的函数

randint():  生成指定范围内(左右边界都包含)随机数(整数)的函数

shuffle():  将列表原地打乱

sample():  从序列中随机指定数量不重复的元素

random():  返回随机生成的一个实数,它在[0,1)范围内

uniform():  生成指定范围内(左边界右边界均不包含)随机数(浮点数)的函数

1 importrandom2 #(0,1)----float 大于0且小于1之间的小数

3 print(random.random())4

5 #[1,3] 大于等于1且小于等于3之间的整数

6 print(random.randint(1, 3))7

8 #[1,3) 大于等于1且小于3之间的整数

9 print(random.randrange(1, 3))10

11 #1或者23或者[4,5]

12 print(random.choice([1, ‘23‘, [4, 5]]))13

14 #列表元素任意2个组合

15 print(random.sample([1, ‘23‘, [4, 5]], 2))16

17 #大于1小于3的小数,如1.927109612082716

18 print(random.uniform(1, 3))

4.random模块应用

(1)验证码

1 #生成一个随机验证码: 前两位是英文字母,后三位是数字

2 importrandom3 checkcode = ‘‘

4

5 for i in range(5):6 current = random.randint(0, 5)7 #字母

8 if i == 0 or i == 1:9 tmp = chr(random.randint(65, 90))10 #数字

11 else:12 tmp = random.randint(0, 9)13 checkcode +=str(tmp)14 print(checkcode)

(2)随机密码生成器

1 importstring2 importrandom3

4 #x中包含了所以的数字、大小写字母和符号

5 x = string.digits + string.ascii_letters +string.punctuation6

7 #random中的choice() -> 是从序列中任意选择一个元素

8 pwd = ‘‘.join([random.choice(x) for i in range(8)])9 print(pwd)

三、re模块

1.re模块的作用: 提供了正则表达式操作所需要的功能

2.正则表达式语法

1 ‘.‘默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行2 ‘^‘ 匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r"^a","\nabc\neee",flags=re.MULTILINE)3 ‘$‘ 匹配字符结尾,或e.search("foo$","bfoo\nsdfsf",flags=re.MULTILINE).group()也可以4 ‘*‘ 匹配*号前的字符0次或多次,re.findall("ab*","cabb3abcbbac") 结果为[‘abb‘, ‘ab‘, ‘a‘]5 ‘+‘ 匹配前一个字符1次或多次,re.findall("ab+","ab+cd+abb+bba") 结果[‘ab‘, ‘abb‘]6 ‘?‘匹配前一个字符1次或0次7 ‘{m}‘匹配前一个字符m次8 ‘{n,m}‘ 匹配前一个字符n到m次,re.findall("ab{1,3}","abb abc abbcbbb") 结果‘abb‘, ‘ab‘, ‘abb‘]9 ‘|‘ 匹配|左或|右的字符,re.search("abc|ABC","ABCBabcCD").group() 结果‘ABC‘

10 ‘(...)‘ 分组匹配,re.search("(abc){2}a(123|456)c", "abcabca456c").group() 结果 abcabca456c11

12

13 ‘\A‘ 只从字符开头匹配,re.search("\Aabc","alexabc") 是匹配不到的14 ‘\Z‘匹配字符结尾,同$15 ‘\d‘ 匹配数字0-9

16 ‘\D‘匹配非数字17 ‘\w‘ 匹配[A-Za-z0-9]18 ‘\W‘ 匹配非[A-Za-z0-9]19 ‘s‘ 匹配空白字符、\t、\n、\r , re.search("\s+","ab\tc1\n3").group() 结果 ‘\t‘

20

3.re模块主要方法

4.正则表达式对象

5.re模块应用

四、time模块

五、datetime模块

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值