Built-in Modules¶
Time module¶
Using time.time() to count how long something takes¶
1 2 3 4 5 6 7 8 9 10 | import time
start = time.time()
for i in range(10000):
continue
new_time = time.time()
total_time = new_time - start
print(total_time)
|
Using time.sleep(n) to wait for n seconds¶
1 2 3 4 5 6 7 8 9 | import time
start = time.time()
time.sleep(10)
end = time.time()
print(start - end)
|
Random Module¶
Generate a random number between 0 and 1¶
1 2 3 4 | import random
num = random.random()
print("the random number is {}".format(num))
|
Generate a random number between two integers¶
1 2 3 4 | import random
num = random.randint(5, 100)
print("the random integer between 5 and 100 is {}".format(num))
|
Select a random item from a list¶
1 2 3 4 5 | import random
my_pets = ['euclid', 'leta']
fav_pet = random.choice(my_pets)
print("My randomly chosen favorite pet is {}".format(fav_pet))
|