import time import random print ("Number of seconds since January 1, 1970: ", time.time()) print () print ("Local Time (as list): ", list(time.localtime())) print ("[YEAR,MONTH,DAY,HOUR,MINUTE,SECOND,DAYOFWEEK,DAYOFYEAR,DAYLIGHTSAVINGTIME]") print () print ("Local Time (as text): ", time.asctime(time.localtime())) # Use time.time() to profile how long a function will take to # execute. Does Task1, which does nothing 10 million times, # take longer time or shorter time than Task2, which uses # time.sleep to wait exactly one-half second? def Task1 (): for I in range(10000000): pass # Do nothing 10 million times (busy wait) return def Task2 (): time.sleep(0.5) # Do nothing for exactly one-half second return def Main(): # Profile how long Task1 takes. Before = time.time() Task1() After = time.time() Elapsed = After - Before print () print ("Task 1 takes ", Elapsed, " seconds") print ("This will be different based on computer's speed.") print ("A faster computer will have a smaller number,") print ("and a slower computer will have a larger number.") # Profile how long Task2 takes. Before = time.time() Task2() After = time.time() Elapsed = After - Before print () print ("Task 2 takes ", Elapsed, " seconds") print ("This will be roughly the same for every computer.") Main() print () for I in range(20): print ("Random number 0<=N<1: ",random.random()) print () for I in range(20): print ("Six-Sided Die #1: ", int(random.random()*6)+1) print () for I in range(20): print ("Six-Sided Die #2: ", random.randrange(6)+1) print () for I in range(20): print ("Six-Sided Die #3: ", random.randint(1,6)) print () for I in range(20): print ("Six-Sided Die #4: ", random.choice([1,2,3,4,5,6]))