Python学习100例之91-100

此Python版本为2.7,其他例子如下:
Python学习100例之1-10
Python学习100例之11-20
Python学习100例之21-30
Python学习100例之31-40
Python学习100例之41-50
Python学习100例之51-60
Python学习100例之61-70
Python学习100例之71-80
Python学习100例之81-90

91.时间函数举例1

import time

print(time.ctime(time.time()))
print(time.asctime(time.localtime(time.time())))
print(time.asctime(time.gmtime(time.time())))

92.时间函数举例2

import time

start = time.time()
for i in range(3000):
    print(i)
end = time.time()

print('different is %6.3f\n' % (end - start))

93.时间函数举例3

import time

start = time.clock()
for i in range(10000):
    print(i)
end = time.clock()
print('different is %6.3f\n' % (end - start))

94.时间函数举例4,一个猜数游戏,判断一个人反应快慢

import time
import random

isPlay = input("Do you want to play it?(\'Y\' or \'N\')')\n")
while isPlay.upper() == 'Y':
    i = random.randint(0, 100 + 1)
    start = time.time()
    guess = int(input("input your guess:"))
    while guess != i:
        if guess > i:
            print("please input a little smaller")
        else:
            print("please input a little bigger")
        guess = int(input("input your guess:"))
    end = time.time()
    print("%fs猜中" % (end - start))
    isPlay = input("Do you want to play it again?(\'Y\' or \'N\')')\n")
print('\n')

95.字符串日期转换为易读的日期格式

import datetime

print(datetime.datetime.strptime('2017-12-27 11:28:06', '%Y-%m-%d %H:%M:%S'), '\n')

96.计算字符串中子串出现的次数

str1 = input("请输入一个字符串:")
str2 = input("请输入该字符串的子串:")
print("字符串中子串出现的次数:%d\n" % str1.count(str2))

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

filename = input("输入文件名:")
fp = open(filename, "w")
character = input("输入字符串:")
while character != '':
    fp.write(character)
    character = input("输入字符串:")
fp.close()
print('\n')

98.从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件"test"中保存

string = input("输入一个字符串:")
fp = open("test", "w")
fp.write(string.upper())
fp.close()
print('\n')

99.有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列), 输出到一个新文件C中

fp1 = open("text1.txt", "w+")
fp1.write('123')
fp1.close()

fp2 = open("text2.txt", "w+")
fp2.write('456')
fp2.close()

fp1 = open("text1.txt", "r")
a = fp1.read()
fp1.close()

fp2 = open("text2.txt", "r")
b = fp2.read()
fp2.close()

l = list(a + b)
l.sort()
s = ''
s = s.join(l)
fp = open("test3.txt", "w+")
fp.write(s)
fp.close()

100.列表转换为字典

i = ['a', 'b']
l = [1, 2]
print(dict(zip(i, l)))

你可能感兴趣的:(Python学习100例之91-100)