1、随机函数
>>> import random
>>> dir(random)
['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']
>>> random.random()
0.2827355995910511
>>> random.random()
0.5864104393643096
>>> int(random.random()*100)
92
>>> random.randint(0,100)
20
>>> random.randint(0,100)
73
>>> random.randint(0,100)
59
2、random.choice() 从序列中随机取一个值
>>> a
[4, 6, 2, 5, 8, 1, 9, 3, 0, 7]
>>> random.choice(a)
9
>>> random.choice(a)
1
>>> random.choice(a)
0
3、random.sample(a,b)从序列a中取出b个元素
>>> import random
>>> a=list(range(10))
>>> random.choice(a)
8
>>> random.choice(a)
7
>>> random.sample(a,3)
[6, 5, 0]
>>> random.sample(a,3)
[2, 1, 9]
>>> random.sample(a,3)
[1, 0, 4]
>>> random.sample(a,3)
[3, 5, 9]
>>> import random
>>> dir(random)
['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']
>>> random.sample("abcdef",3)
['f', 'a', 'e']
>>> random.sample("abcdef",3)
['c', 'd', 'f']
>>> random.sample("abcdef",3)
['b', 'f', 'a']
>>> random.sample("abcdef",10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\sunqianli\AppData\Local\Programs\Python\Python36\lib\random.py", line 318, in sample
raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative
import sys
print(1)
sys.exit()
print(2)
4、浮点运算结果简化
>>> 10-9.9
0.09999999999999964
>>> (10*100-9.9*100)/100
0.1
>>> (10*10-9.9*10)/10
0.1
>>> 123.3/100
1.2329999999999999
>>> (123.3*10)/(100*10)
1.233
>>> (123.3*100)/(100*100)
1.233
5、启动python的调试器pdb:python -m pdb a.py
import sys
a=1
b=2
print(a+b)
c=a*b
6、pdb.set_trace()这个方法也是用pdb,但是不需要单步执行,我们只需要import pdb,然后,在可能出错的地方放一个pdb.set_trace(),就可以设置一个断点。
import pdb
s="0"
n=list(s)
pdb.set_trace()#运行到这里会自动暂停
print(10/n)