python模塊2 math andom e imedatetime模塊
阿新 • • 發佈:2018-04-07
列表 tmp shuf 換行 seed 三角函數 shuff letters range
知識內容:
1.math模塊
2.random模塊
3.re模塊
4.time模塊
5.datetime模塊
一、math模塊
1.math模塊的作用: 它提供了一些基本的數學運算的函數,比如說對數、三角函數、開發、乘方等
2.math模塊中的內容
1 >>> import math 2 >>> 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
返回常數:
import math print(math.pi) print(math.e)
(2)求對數
log(x, a) # 如果不寫a默認為e
log10(x)
1 >>> import math 2 >>> 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 >>> import math 2 >>> 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 >>> import random 2 >>> 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 import random 2 # (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 import random 3 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 import string 2 import random 3 4 # x中包含了所以的數字、大小寫字母和符號 5 x = string.digits + string.ascii_letters + string.punctuation 6 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() 結果 abcabca456c 11 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模塊
python模塊2 math\random\re\time\datetime模塊