基础语法
import random
ran = random.random() #得到0~1之间的随机小数
print(ran)
ran = random.randrange(1,10)
print(ran)
ran = random.randrange(1,10,2)
print(ran)
ran = random.randint(1,10) #1~10数字
print(ran)
list1 = ['学强','飞飞','家伟','鹏','阿文']
ran = random.choice(list1) #随机选择列表的内容
print(ran)
pai = ['红桃A','方片A','黑桃4']
result = random.shuffle(pai) #打乱列表中元素的顺序
print(pai)
验证码 大写字母与数字的组合
import random
def func():
code = ''
for i in range(4):
ran1 = str(random.randint(0,9))
ran2 = chr(random.randint(65,90))
ran3 = chr(random.randint(97,122))
r = random.choice([ran1,ran2,ran3])
code+=r
return code
code = func()
print(code)
补充
chr 给一个数字就把数字转为ASCII码的字母 Unicode码 —》 str
ord 给一个字符就把字符转为ASCII码的数字 str ------》 Unicode码
加密算法,加密越长则保密性越好!
import hashlib
msg = 'tomy ,中午一起吃饭去'
md5 = hashlib.md5(msg.encode('utf-8')) #不可逆的
print(md5.hexdigest())
md5_1 = hashlib.md5(msg.encode('utf-8'))
print(md5_1.hexdigest())
sha1 = hashlib.sha1(msg.encode('utf-8'))
print(sha1.hexdigest())
print(len(sha1.hexdigest()))
sha256 = hashlib.sha256(msg.encode('utf-8'))
print(sha256.hexdigest())
print(len(sha256.hexdigest()))
#输出结果为:
e5bd42ab33a6661b948f5ff5c0b95452
e5bd42ab33a6661b948f5ff5c0b95452
5502856acffc3088f173df071cac556b76bc94fe
40
50d3366d3b7702ec7bd088a72b50c6feaea65bdafe764d3be4175117643e6edb
64
两种安装方式:一个直接pip ,另一个在pycharm中进入File | Settings | Project: suanFaJingSai | Project Interpreter 点击’+’,搜索自己所需的模块!
小发现: pycharm可以进入dos窗,点击Terminal中的local.