Python标准库--random,sys,time

来源:互联网 发布:繁体字转换简体字 mac 编辑:程序博客网 时间:2024/05/16 17:49

常用的Python标准库有好多,Python安装目录下的Lib文件夹里有这些标准库的源码。我的天啊,每一个源码文件都值得好好研究。

1 random

这里只用到了random模块中的randint()方法,可以得到0-100范围内的一个整数。然后,是一个猜数字的游戏。

import randomPC_guess = random.randint(0,100)print("Guess an int between 0-100")print('*'*50)counter = 0while True:    counter = counter +1    user_guess = int(input("Enter your number:"))    print('*'*50)    if user_guess > PC_guess:        print("Guess a lower number")    elif user_guess < PC_guess:        print("Guess a higher number")    else:        print("You got it!")        breakprint('It\'s takes you ',counter,' times to get it. Good job.') RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36-32/Guessing_Game.py Guess an int between 0-100**************************************************Enter your number:50**************************************************Guess a lower numberEnter your number:25**************************************************Guess a lower numberEnter your number:13**************************************************Guess a lower numberEnter your number:6**************************************************Guess a lower numberEnter your number:3**************************************************Guess a higher numberEnter your number:4**************************************************You got it!It's takes you  6  times to get it. Good job.

2 sys

sys — System-specific parameters and functions

sys 模块中常用的有argv(命令行参数)、stdin(标准输入流)、stdout(标准输出流)、stderr(标准错误流)方法。

2.1 argv
这里写图片描述

2.2 stdin,stdout,stderr

File objects used by the interpreter for standard input, output and errors:    ○ stdin is used for all interactive input (including calls to input());    ○ stdout is used for the output of print() and expression statements and for the prompts of input();    ○ The interpreter’s own prompts and its error messages go to stderr.

https://docs.python.org/3/library/sys.html

3 time

time模块很有用啊,

>>> import time>>> time.time()1499153518.0803897  #从1970年1月1日算起>>> time.asctime<built-in function asctime>>>> time.asctime()'Tue Jul  4 15:42:40 2017'>>> now = (2017,7,4,15,46,30,2,0,0)>>> time.asctime(now)'Wed Jul  4 15:46:30 2017'>>> time.localtime()time.struct_time(tm_year=2017, tm_mon=7, tm_mday=4, tm_hour=15, tm_min=49, tm_sec=57, tm_wday=1, tm_yday=185, tm_isdst=0)>>> t = time.localtime()>>> yeat = t[0]>>> year = t[0]>>> month = t[1]>>> day = t[2]>>> year2017>>> day4>>> month7>>> print(day,'/',month,'/',year)4 / 7 / 2017>>> print(str(day) + '/' + str(month) + '/' + str(year))4/7/2017

time.time()方法可以用来计算一个程序的运行时间。
比如,把前面的质数生成器代码改写一下。

>>> import time>>> def prime_number(num1,num2):    """Find all prime number between num1 and num2.    Use prime_number(num1,num2) command to run it.    When executed,it will return a list of prime numbers and the time used    for executing the program.    """    Prime_number = []    t1 = time.time()    for i in range(num1,num2):        j = 2        counter = 0        while j<i:            if i%j == 0:                counter = 1                j = j +1            else:                j = j + 1        if counter == 0:            Prime_number.append(i)        else:            counter = 0        t2 = time.time()        detla_time = t2 - t1        msg = "Program Executing Time: " + str(detla_time) + 'seconds.'    return Prime_number ,'/n',msg>>> prime_number(2,200)([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199], '/n', 'Program Executing Time: 0.007000446319580078 seconds.')>>> 

还有sleep()方法。
下面在for循环内加了time.sleep(0.5) 代码,程序总运行时间多了大概99s。
爬虫的时候会用到,有些网站会限制一定时间内的请求次数。
还没系统学习过爬虫,到时候要记着这个方法啊。

>>> import time>>> def prime_number(num1,num2):    """Find all prime number between num1 and num2.    Use prime_number(num1,num2) command to run it.    When executed,it will return a list of prime numbers and the time used    for executing the program.    """    Prime_number = []    t1 = time.time()    for i in range(num1,num2):        time.sleep(0.5)  # 休息0.5s再运行代码        j = 2        counter = 0        while j<i:            if i%j == 0:                counter = 1                j = j +1            else:                j = j + 1        if counter == 0:            Prime_number.append(i)        else:            counter = 0        t2 = time.time()        detla_time = t2 - t1        msg = "Program Executing Time: " + str(detla_time) + ' seconds.'    return Prime_number ,'/n',msg>>> prime_number(2,200)([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199], '/n', 'Program Executing Time: 99.23054504394531 seconds.')>>> 
原创粉丝点击