Skip to content
Learnearn.uk » Python Unit Home » Python Random Module Tutorial

Python Random Module Tutorial

Cheat Sheet

import random

#Generate a random whole number between 0 and 6

num1 = random.randint(0,6)

 

#Choose a random item from  a list

animals = ['dog','cat','mouse']

choice = random.choice(animals)

 

#Choose a random item from a list and remove it from the list

index = random.randint(0,len(animals)-1)

removed  = animals.pop(index)

 

#Shuffle a list

random.shuffle(animals)