Home >>Python Random Module >Python Random sample() Method

Python Random sample() Method

Python Random sample() Method

Python Random sample() Method is an inbuilt function in python which is used to return a particular list of items chosen from a sequence (like string, list, tuple).

Syntax:
random.sample(sequence, k)

Parameter Values

Parameter Description
sequence It is Required a sequence of items.
k It Required the returned list size.
Here is an example of Python Random sample() Method:

import random
mygame = ["football", "cricket", "vollyball"]
print(random.sample(mygame, k=2))

Output:
['football', 'vollyball']
Example 2:

import random
mygame = ["football", "cricket", "vollyball"]
print(random.sample(mygame, k=1))

Output:
['vollyball']

No Sidebar ads