python全天课视频(3)

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)

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值