首页 > 技术文章 > 计算机二级python 知识点篇(python标准库)

BlogOfEr 2019-08-30 11:15 原文

计算机二级python 知识点篇(python标准库)

标准库: turtle库(必选)

标准库: random库(必选)、 time库(可选)

turtle库

窗体函数

turtle.setup(width, height, startx, starty)
width : 窗口宽度, 如果值是整数,表示的像素值;如果值是小数,表示窗口宽度与屏幕的比例;
height: 窗口高度, 如果值是整数, 表示的像素值; 如果值是小数,表示窗口高度与屏幕的比例;
startx: 窗口左侧与屏幕左侧的像素距离, 如果值是None, 窗口位于屏幕水平中央;
starty: 窗口顶部与屏幕顶部的像素距离, 如果值是None, 窗口位于屏幕垂直中央;

画笔状态函数

函数 描述
pendown() 放下画笔
penup() 提起画笔, 与pendown()配对使用
pensize(width) 设置画笔线条的粗细为指定大小
color() 设置画笔的颜色
begin_fill() 填充图形前, 调用该方法
end_fill() 填充图形结束
filling() 返回填充的状态, True为填充, False为未填充
clear() 清空当前窗口, 但不改变当前画笔的位置
reset() 清空当前窗口, 并重置位置等状态为默认值
screensize() 设置画布的长和宽
hideturtle() 隐藏画笔的turtle形状
showturtle() 显示画笔的turtle形状
isvisible() 如果turtle可见, 则返回True

画笔运动函数

函数 描述
forward(distance) 沿着当前方向前进指定距离
backward(distance) 沿着当前相反方向后退指定距离
right(angle) 向右旋转angle角度
left(angle) 向左旋转angle角度
goto(x,y) 移动到绝对坐标(x,y) 处
setx( ) 将当前x轴移动到指定位置
sety( ) 将当前y轴移动到指定位置
setheading(angle) seth() 设置当前朝向为angle角度
home() 设置当前画笔位置为原点, 朝向东。
circle(radius,e) 绘制一个指定半径r和角度e的圆或弧形
circle(r,steps= n) 绘制n边型 python3
dot(r,color) 绘制一个指定半径r和颜色color的圆点
undo() 撤销画笔最后一步动作
speed() 设置画笔的绘制速度, 参数为0-10之间

random 库

random库概述

  • 使用random库主要目的是生成随机数
  • 这个库提供了不同类型的随机数函数, 其中最基本的函数是random.random(), 它生成一个(0.0, 1.0)之间的随机小数, 所有其他随机函数都是基于这个函数扩展而来。
>>>from random import *
>>>random()
0.5780913011344704
>>>random()
0.20609823213950174

random库的常用函数

函数 描述
seed(a=None) 描初始化随机数种子, 默认值为当前系统时间
random() 描生成一个(0.0, 1.0)之间的随机小数
randint(a, b) 描生成一个[a,b]之间的整数
getrandbits(k) 描生成一个k比特长度的随机整数
randrange(start, stop[, step]) 描生成一个[start, stop)之间以step为步数的随机整数
uniform(a, b) 描生成一个[a, b]之间的随机小数
choice(seq) 描从序列类型(例如: 列表)中随机返回一个元素
shuffle(seq) 描将序列类型中元素随机排列, 返回打乱后的序列
sample(pop, k) 描从pop类型中随机选取k个元素, 以列表类型返回

random库与随机数运用

  • random库使用random.seed(a)对后续产生的随机数设置种子a。
  • 设置随机数种子的好处是可以准确复现随机数序列, 用于重复程序的运行轨迹。 对于仅使用随机数但不需要复现的情形, 可以不用设置随机数种子。
  • 如果程序没有显式设置随机数种子, 则使用随机数生成函数前, 将默认以当前系统的运行时间为种子产生随机序列。
>>>from random import *
>>>seed(10)
>>>random()
0.5714025946899135
>>>random()
0.4288890546751146
>>>seed(10) #再次设置相同的种子, 则后续产生的随机数相同
>>>random()
0.5714025946899135
>>>random()
0.4288890546751146
>>> from random import *
>>> random()
0.5804600783692248
>>> uniform(1,10)
5.341789197382285

>>> randint(1,10)
5

>>> choice(list(range(10)))
1
l =list(range(10))
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> shuffle(l)
>>> l
[6, 8, 1, 3, 9, 7, 0, 2, 4, 5]
>>> sample(l,3)
[4, 1, 9]
>>> sample("hello world",5)
['d', 'l', 'e', 'o', 'o']
>>> randrange(1,100,2)
53
>>> randrange(0,100,2)
92
>>> for _ in range(10):
	randint(0,100)

	
90
6
49
14
56
55
31
71
65
90
>>> str = "abcdefghij"
>>> sample(str,4)
['b', 'e', 'j', 'a']
>>> ll = ['apple','pear','peach','orange']
>>> sample(ll,1)
['apple']
>>> sample(ll,1)
['orange']

time 库

time库概述

  • 处理时间是程序最常用的功能之一, time库是Python提供的处理时间标准库。
  • time库的功能主要分为3个方面: 时间处理、时间格式化和计时。

时间处理

时间处理主要包括4个函数:

函数 作用
time.time() 获取当前时间戳
time.gmtime() 获取当前时间戳对应的struct_time对象
time.localtime() 获取当前时间戳对应的本地时间的struct_time对象,与上面一个的不同点是获取的是北京时间
time.ctime() 获取当前时间戳对应的易读字符串表示,内部会调用time.localtime()函数以输出当地时间
>>>import time
>>>time.time()
1516939876.6022282

>>> time.gmtime(now)
time.struct_time(tm_year=2018, tm_mon=1,tm_mday=26, tm_hour=4, tm_min=11, tm_sec=16,tm_wday=4, tm_yday=26, tm_isdst=0)

>>> time.localtime(now)
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=26,tm_hour=12, tm_min=11, tm_sec=16, tm_wday=4,tm_yday=26, tm_isdst=0)

>>> time.ctime(now)
'Fri Jan 26 12:11:16 2018'

时间格式化

时间格式化包含三个函数:

函数 作用
time.mktime() 将struct_time对象t转换为时间戳
time.strftime() 方法利用一个格式字符串, 对时间格式进行表达
time.strptime() strptime()方法与strftime()方法完全相反, 用于提取字符串中时间来生strut_time对象
>>> t = time.localtime(now)
>>> time.mktime(t)
1516939876.0
>>> time.ctime(time.mktime(t))
'Fri Jan 26 12:11:16 2018'

>>> lctime = time.localtime()
>>> lctime
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=26,tm_hour=12, tm_min=55, tm_sec=20, tm_wday=4, tm_yday=26,tm_isdst=0)
>>> time.strftime("%Y-%m-%d %H:%M:%S", lctime)
'2018-01-26 12:55:20


>>> timeString = '2018-01-26 12:55:20'
>>> time.strptime(timeString, "%Y-%m-%d %H:%M:%S")
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=26,tm_hour=12, tm_min=55, tm_sec=20,tm_wday=4,tm_yday=26,tm_isdst=-1)
  • strftime()方法的格式化控制符
格式化字符串 日期/时间 值范围和实例
%Y 年份 0001~9999, 例如: 1900
%m 月份 01~12, 例如: 10
%B 月名 January~December, 例如: April
%b 月名缩写 Jan~Dec, 例如: Apr
%d 日期 01 ~ 31, 例如: 25
%A 星期 Monday~Sunday, 例如: Wednesday
%a 星期缩写 Mon~Sun, 例如: Wed
%H 小时(24h制) 00 ~ 23, 例如: 12
%I 小时(12h制) 01 ~ 12, 例如: 7
%p 上/下午 AM, PM, 例如: PM
%M 分钟 00 ~ 59, 例如: 26
%S 00 ~ 59, 例如: 26

程序运行时间计时

import time
def coreLoop():
  limit = 10**8
  while (limit > 0):
  limit -= 1
def otherLoop1():
  time.sleep(0.2)
def otherLoop2():
  time.sleep(0.4)
  
def main():
  startTime = time.localtime()
  print('程序开始时间: ', time.strftime('%Y-%m-%d %H:%M:%S', startTime))
  startPerfCounter = time.perf_counter()
  otherLoop1()
  otherLoop1PerfCounter = time.perf_counter()
  otherLoop1Perf = otherLoop1PerfCounter - startPerfCounter
  coreLoop()
  coreLoopPerfCounter = time.perf_counter()
  coreLoopPerf = coreLoopPerfCounter - otherLoop1PerfCounter
  otherLoop2()
  otherLoop2PerfCounter = time.perf_counter()
  otherLoop2Perf = otherLoop2PerfCounter - coreLoopPerfCounter
  endPerfCounter = time.perf_counter()
  totalPerf = endPerfCounter - startPerfCounter
  endTime = time.localtime()
  print("模块1运行时间是:{}秒".format(otherLoop1Perf))
  print("核心模块运行时间是:{}秒".format(coreLoopPerf))
  print("模块2运行时间是:{}秒".format(otherLoop2Perf))
  print("程序运行总时间是:{}秒".format(totalPerf))
  print('程序结束时间: ', time.strftime('%Y-%m-%d %H:%M:%S', endTime))
main()

'''
程序开始时间: 2017-12-26 13:46:39
模块1运行时间是:0.20003105182731706秒
核心模块运行时间是:5.987101639820927秒
模块2运行时间是:0.40018931343066555秒
程序运行总时间是:6.587323585324574秒
程序结束时间: 2017-12-26 13:46:45
'''

推荐阅读