Python练手例子(16)

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:\n')
        i = random.randint(0, 2 ** 32) % 100
        print('Please input number you guess:\n')
        start = time.perf_counter()
        a = time.time()
        guess = int(input('Input your guess:\n'))
        while guess != i:
            if guess > i:
                print('Please input a little smaller')
                guess = int(input('Input your guess:\n'))
            else:
                print('Please input a little bigger.')
                guess = int(input('Input your guess:\n'))
        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('请输入一个字符串:\n')
    str2 = input('请输入一个字符串:\n')
    ncount = str1.count(str2)
    print(ncount)

 

97、从键盘输入一些字符,逐个把它们写到磁盘文件上,直到输入一个“#”为止。

#!/usr/bin/python
#coding=utf-8

from sys import stdout
if __name__ == '__main__':
    filename = input('输入文件名:\n')
    fp = open(filename, 'w')
    ch = input('输入字符串:\n')
    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:\n')
    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例

posted @ 2019-02-26 18:44  finsom  阅读(542)  评论(0编辑  收藏  举报