Python practice example (16)

Keywords: Python

91. Time function example 1.

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

import time
if __name__ == '__main__':
    #time.time()Returns the current timestamp (floating-point seconds since the 1970s)
    print(time.time())
    #time.ctime()Convert timestamps to time.asctime()Form
    print(time.ctime(time.time()))
    #time.asctime()Return"Tue Feb 26 09:12:37 2019"24 strings of
    #time.localtime()Format time stamp as local time
    print(time.asctime(time.localtime(time.time())))
    #time.gmtime()Get the current time format that other computers can process
    print(time.asctime(time.gmtime(time.time())))

//Result:
1551143557.8197014
Tue Feb 26 09:12:37 2019
Tue Feb 26 09:12:37 2019
Tue Feb 26 01:12:37 2019

 

92. Time function example 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. Time function example 3.

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

import time
if __name__ == '__main__':
    #time.clock()Returns the current CPU time
    #time.clock()stay Pyhon3.3 Abandoned in Pyhon3.8 Will be removed in Pyhon3.7 It will alarm when it is used in. It is recommended to use 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. Time function example 4: a guessing game to judge a person's response speed.

#!/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. Convert string date to readable date format.

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

#Need to install dateutil Modular
from dateutil import parser
dt = parser.parse('Feb 26 2019 10:00AM')
print(dt)

 

96. Count the number of substrings in the string.

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

if __name__ == '__main__':
    str1 = input('Please enter a string:\n')
    str2 = input('Please enter a string:\n')
    ncount = str1.count(str2)
    print(ncount)

 

97. Input some characters from the keyboard and write them to the disk file one by one until you input a "ා".

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

from sys import stdout
if __name__ == '__main__':
    filename = input('Enter file name:\n')
    fp = open(filename, 'w')
    ch = input('Input string:\n')
    while ch != '#':
        fp.write(ch)
        stdout.write(ch)
        ch = input('')
    fp.close()

 

98. Input a string from the keyboard, convert all lowercase letters to uppercase letters, and then output to a "test" to save.

#!/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. There are two disk files A and B, each with A line of letters. It is required to merge the information in these two files (in alphabetical order) and output it to A new file C.

Note: files A and B must be in the same directory as 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()

//Result:
Favourite
GreenBook
BFGaeeeiknooorrtuv

 

100. Convert the list to a dictionary.

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

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

 

 

 

reference material:

Python 100 cases

Posted by coderWil on Tue, 10 Dec 2019 01:43:09 -0800