sample() is an inbuilt function of random module in Python that returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement. Syntax : random.sample(sequence, k) Parameters: sequence: Can be a list, tuple, string, or set. k: An Integer value, it specify the length of a sample.
Returns: k length new list of elements chosen from the sequence.
Code #1: Simple implementation of sample() function. filter_none edit play_arrow brightness_4 # Python3 program to demonstrate # the use of sample() function .
# import random from random import sample
# Prints list of random items of given length
list1 = [1, 2, 3, 4, 5]
print(sample(list1,3))
/conversion/tmp/scratch/449059089.doc Page 1 of 1 11/26/19 f