python3基础题目,Python3.x 基础练习题100例(91-100)
練習91:
題目:
時間函數舉例1。
程序:
if __name__ == '__main__':
import time
print (time.ctime(time.time()))
print (time.asctime(time.localtime(time.time())))
print (time.asctime(time.gmtime(time.time())))
輸出結果:
Sat Mar 23 19:37:22 2019
Sat Mar 23 19:37:22 2019
Sat Mar 23 11:37:22 2019
練習92:
題目:
時間函數舉例2。
程序:
if __name__ == '__main__':
import time
start = time.time()
for i in range(3000):
print(i)
end = time.time()
print(end - start)
輸出結果:
0
1
2
。
。
。
2997
2998
2999
0.02006244659423828
練習93:
題目:
時間函數舉例3。
程序:
if __name__ == '__main__':
import time
start = time.time()
for i in range(10000):
print(i)
end = time.time()
print('different is %6.3f' % (end - start))
輸出結果:
0
1
2
3
。
。
。
9996
9997
9998
9999
different is 0.059
練習94:
題目:
時間函數舉例4,一個猜數游戲,判斷一個人反應快慢。
程序:
if __name__ == '__main__':
import time
import random
play_it = input('do 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:')
start = time.time()
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.time()
b = time.time()
var = (end - start) / 18.2
print(var)
# print 'It took you %6.3 seconds' % time.difftime(b,a))
if var < 15:
print('you are very clever!')
elif var < 25:
print('you are normal!')
else:
print('you are stupid!')
print('Congradulations')
print('The number you guess is %d' % i)
play_it = input('do you want to play it.')
練習95:
題目:
字符串日期轉換為易讀的日期格式。
程序:
from dateutil import parser
dt = parser.parse("Aug 28 2015 12:00AM")
print (dt)
練習96:
題目:
計算字符串中子串出現的次數。
程序:
if __name__ == '__main__':
str1 = input('請輸入一個字符串:')
str2 = input('請輸入一個子字符串:')
ncount = str1.count(str2)
print(ncount)
輸出結果:
請輸入一個字符串:qwery gdfdaabbbcccabc
請輸入一個子字符串:a
3
練習97:
題目:
從鍵盤輸入一些字符,逐個把它們寫到磁盤文件上,直到輸入一個 # 為止。
程序:
if __name__ == '__main__':
filename = input('輸入文件名:')
fp = open(filename,"w")
ch = input('輸入字符串:')
while ch != '#':
fp.write(ch)
print(ch)
ch = input('')
fp.close()
練習98:
題目:
從鍵盤輸入一個字符串,將小寫字母全部轉換成大寫字母,然后輸出到一個磁盤文件"test"中保存。
程序:
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中。
程序:
if __name__ == '__main__':
import string
fp = open('text1.txt')
a = fp.read()
fp.close()
fp = open('text2.txt')
b = fp.read()
fp.close()
fp = open('text3.txt', 'w')
l = list(a + b)
l.sort()
s = ''
s = s.join(l)
fp.write(s)
fp.close()
練習100:
題目:
列表轉換為字典。
程序:
i = ['a', 'b']
l = [1, 2]
print(dict([i, l]))
輸出結果:
{'a': 'b', 1: 2}
總結
以上是生活随笔為你收集整理的python3基础题目,Python3.x 基础练习题100例(91-100)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php header x-auth-to
- 下一篇: anaconda python2.7,安