More Modules
Python has lots of built-in modules that can help you out. I'll discuss a few of them here.
Python has lots of built-in modules that can help you out. I'll discuss a few of them here.
The time
module contains functions related to date and time.
The time.sleep(timeInSeconds)
function lets you pause a program for a few seconds. For example, to pause a program for 5 seconds, use:
import time
time.sleep(5) # This pauses the program for 5 seconds.
The random
module contains functions that you can use for randomness.
The random.randint(lower,upper)
function will returns a random integer between lower and upper. For example, to get a random number between 1 and 10, use:
import random
print(random.randint(1,10)) # This will print a random number from 1 to 10.
The random.choice(list/tuple/set)
function will return a random item from list/tuple/set. For example, to return a random fruit from the list of ["apples","bananas","oranges","pears"]
, use:
import random
fruits = ["apples","bananas","oranges","pears"]
print(random.choice(fruits)) # This will print a random item from the list fruits.
The random.shuffle(list/tuple)
function will shuffle list/tuple, then return the shuffled list.
sys
Modulemath
Modulesys.exit()
, not break
.