Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
31 lines (22 loc) · 887 Bytes

File metadata and controls

31 lines (22 loc) · 887 Bytes
Copy raw file
Download raw file
Outline
Edit and raw actions

The Random Module

Random Element: random.choice(x)

The random.choice(x) function returns a random element of a list, or a random character of a string.

import random
print(random.choice(['apples', 'bananas', 'pears'])) # randomly prints apples, bananas, or pears
print(random.choice('abc')) # randomly prints a, b, or c

Random Integer: random.randint(x, y)

The random.randint(x, y) function returns a random integer between x and y (inclusive - it can be x or y as well).

import random
print(random.randint(5, 10)) # randomly prints 5, 6, 7, 8, 9, or 10

Shuffle: random.shuffle(x)

The random.shuffle(x) module randomly shuffles a list in-place (it does not return a new list).

import random
fruits = ['apples', 'bananas', 'pears']
random.shuffle(fruits)
print(fruits) # e.g. ['bananas', 'pears', 'apples']
Morty Proxy This is a proxified and sanitized view of the page, visit original site.