首页 > 代码库 > python学习之案例

python学习之案例

1、验证码生成案例:

#!/usr/bin/env python

import random
code=[]

for i in range(6):
    if i == random.randint(1,5): 
        code.append(str(random.randint(1,9)))
    else:
        temp = random.randint(65,90)
        code.append(chr(temp))  #chr转化成字母
print(‘‘.join(code))  #格式化成字符串

 

2、线程和进程对比

from multiprocessing import Process,Queue
import threading

def run(info_list,n):
    info_list.append(n)
    print(info_list)

info = []
if __name__ == ‘__main__‘:
    for i in range(12):
        p = Process(target=run,args=[info,i])
        p.start()

print(‘--------------------------‘)

if __name__ == ‘__main__‘:
    for i in range(12):
        p = threading.Thread(target=run, args=[info, i])
        p.start()

 

python学习之案例