計算機二級python 知識點篇(python標准庫)


計算機二級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
'''


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM