91、時間函式舉例1。
#!/usr/bin/python #coding=utf-8 import time if __name__ == `__main__`: #time.time()返回當前的時間戳(1970紀元後經過的浮點秒數) print(time.time()) #time.ctime()把時間戳轉化為time.asctime()的形式 print(time.ctime(time.time())) #time.asctime()返回"Tue Feb 26 09:12:37 2019"的24個字串 #time.localtime()格式化時間戳為本地的時間 print(time.asctime(time.localtime(time.time()))) #time.gmtime()獲取別的計算機可以處理的當前時間格式 print(time.asctime(time.gmtime(time.time()))) 結果: 1551143557.8197014 Tue Feb 26 09:12:37 2019 Tue Feb 26 09:12:37 2019 Tue Feb 26 01:12:37 2019
92、時間函式舉例2。
#!/usr/bin/python #coding=utf-8 import time if __name__ == `__main__`: start = time.time() for i in range(3000): print(i) end = time.time() print(end - start)
93、時間函式舉例3。
#!/usr/bin/python #coding=utf-8 import time if __name__ == `__main__`: #time.clock()以浮點數計數的秒數返回當前的CPU時間 #time.clock()在Pyhon3.3被廢棄,在Pyhon3.8中將被移除,在Pyhon3.7中使用會報警,建議使用time.perf_counter() start = time.perf_counter() for i in range(10000): print(i) end = time.perf_counter() print(`Different is %6.3f` % (end - start))
94、時間函式舉例4:一個猜數遊戲,判斷一個人的反應快慢。
#!/usr/bin/python #coding=utf-8 import time import random if __name__ == `__main__`: play_it = input(`Dou you want to play it? (`y` or `n`)`) while play_it == `y`: c = input(`Input a character: `) i = random.randint(0, 2 ** 32) % 100 print(`Please input number you guess: `) start = time.perf_counter() a = time.time() guess = int(input(`Input your guess: `)) while guess != i: if guess > i: print(`Please input a little smaller`) guess = int(input(`Input your guess: `)) else: print(`Please input a little bigger.`) guess = int(input(`Input your guess: `)) end = time.perf_counter() b = time.time() var = (end - start) / 18.2 print(`It took you %6.3f seconds.` % var) if var < 15: print(`You are very clever!`) elif var < 25: print(`You are normal.`) else: print(`Well, you have to refuel.`) print(`Congradulations!`) print(`The number you guess is %d` % i) play_it = input(`Do you want to play it again?`)
95、字串日期轉換為易讀的日期格式。
#!/usr/bin/python #coding=utf-8 #需要安裝dateutil模組 from dateutil import parser dt = parser.parse(`Feb 26 2019 10:00AM`) print(dt)
96、計算字串中子串出現的次數。
#!/usr/bin/python #coding=utf-8 if __name__ == `__main__`: str1 = input(`請輸入一個字串: `) str2 = input(`請輸入一個字串: `) ncount = str1.count(str2) print(ncount)
97、從鍵盤輸入一些字元,逐個把它們寫到磁碟檔案上,直到輸入一個“#”為止。
#!/usr/bin/python #coding=utf-8 from sys import stdout if __name__ == `__main__`: filename = input(`輸入檔名: `) fp = open(filename, `w`) ch = input(`輸入字串: `) while ch != `#`: fp.write(ch) stdout.write(ch) ch = input(``) fp.close()
98、從鍵盤輸入一個字串,將小寫字母全部轉換成大寫字母,然後輸出到一個“test”中儲存。
#!/usr/bin/python #coding=utf-8 if __name__ == `__main__`: fp = open(`test.txt`, `w`) string = input(`Please input a string: `) string = string.upper() fp.write(string) fp = open(`test.txt`, `r`) print(fp.read()) fp.close()
99、有兩個磁碟檔案A和B,各放一行字母,要求把這兩個檔案中的資訊合併(按字母順序排列),輸出到一個新的檔案C中。
注:必須將檔案A和B放在99.py同一個目錄下
#!/usr/bin/python #coding=utf-8 import string if __name__ == `__main__`: fp = open(`test1.txt`) a = fp.read() print(a) fp.close() fp = open(`test2.txt`) b = fp.read() print(b) fp.close() fp = open(`test.txt`, `w`) l = list(a + b) l.sort() s = `` s = s.join(l) print(s) fp.write(s) fp.close() 結果: Favourite GreenBook BFGaeeeiknooorrtuv
100、列表轉換為字典。
#!/usr/bin/python #coding=utf-8 i = [`a`, `b`] l = [1, 2] print(dict([i,l]))
參考資料:
Python 100例