So this is my second extra blog. Like last time extra blog, i want to show an interesting Python code that i discovered. So here is the code:
import time
def fun():
start = time.time()
a = 'ynnuf yrev era uoy'
for i in range(len(a)-1, -1, -1):
print(a[i])
time.sleep(1)
print('The for loop takes: ' + str(time.time()-start) + 'to run')
So if you run this code, the program will print a message that you may or may not recognize yet, haha! But the point is not the message, but the way it print the message, normally we print the entire message at once but by using the method time.sleep('time in second'), i can delay my print statement each time the loop is executed, generating some sorts of animation. Moreover, i can also calculate the time taken for the program to execute my for loop using the method time.time()!
No comments:
Post a Comment