Python Program for sample() Function with Examples

When handling problems involving data prediction, we frequently encounter scenarios in which we must test the algorithm on a small set of data to evaluate the method’s accuracy.

This is where the Python sample() function comes into play.

For operations, we may use the sample() method to select a random sample from the available data. Though there are other strategies for sampling data, the sample() method is widely regarded as one of the most simple.

Python’s sample() method works with all sorts of iterables, including list, tuple, sets, dataframes, and so on. It selects data from the iterable at random from the user-specified number of data values.

sample() Function:

sample() is a built-in function of Python’s random module that returns a specific length list of items taken from a sequence, such as a list, tuple, string, or set. Used for random sampling Non-replacement.

Syntax:

random.sample(sequence, k)

Parameters:

sequence: It may be a list, tuple, string, or set, etc.

k: It is an Integer. This is the length of a sample.

Return Value:

Returns a new list of k elements selected from the sequence.

Examples:

1)For Lists

Approach:

  • Import sample() function from the random module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the length of the sample as static input and store it in another variable.
  • Pass the given list, sample length as the arguments to the sample() method to get the given length(len_sampl) of random sample items from the list.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import sample() function from the random module using the import keyword.
from random import sample
# Give the list as static input and store it in a variable.
gvn_lst = [24, 4, 5, 9, 2, 1]
# Give the length of the sample as static input and store it in another variable.
len_sampl = 3
# Pass the given list, sample length as the arguments to the sample() method
# to get given length(len_sampl) of random sample items from the list
# Store it in another variable.
rslt = sample(gvn_lst, len_sampl)
# Print the above result.
print("The", len_sampl, "random items from the given list are:")
print(rslt)

Output:

The 3 random items from the given list are:
[9, 24, 4]
2)For Sets

Approach:

  • Import sample() function from the random module using the import keyword.
  • Give the set as static input and store it in a variable.
  • Give the length of the sample as static input and store it in another variable.
  • Pass the given set, sample length as the arguments to the sample() method to get the given length(len_sampl) of random sample items from the set.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import sample() function from random module using the import keyword.
from random import sample
# Give the set as static input and store it in a variable.
gvn_set = {2, 4, 7, 1, 3, 1, 4, 2}
# Give the length of sample as static input and store it in another variable.
len_sampl = 4
# Pass the given set, sample length as the arguments to the sample() method
# to get given length(len_sampl) of random sample items from the set
# Store it in another variable.
rslt = sample(gvn_set, len_sampl)
# Print the above result.
print("The", len_sampl, "random items from the given set are:")
print(rslt)

Output:

The 4 random items from the given set are:
[1, 4, 7, 2]
Exceptions & Errors While using the sample() Function

When working with the sample() function, we may see a ValueError exception. This exception is thrown if the sample length is greater than the length of the iterable.

For Example:

# Import sample() function from the random module using the import keyword.
from random import sample
# Give the list as static input and store it in a variable.
gvn_lst = [24, 4, 5, 9, 2, 1]
# Give the length of the sample as static input and store it in another variable.
len_sampl = 10
# Pass the given list, sample length as the arguments to the sample() method
# to get given length(len_sampl) of random sample items from the list
# Store it in another variable.
rslt = sample(gvn_lst, len_sampl)
# Print the above result.
print("The", len_sampl, "random items from the given list are:")
print(rslt)

Output:

Traceback (most recent call last):
  File "/home/f39024cad259bfac4456fa81ec86fb82.py", line 10, in <module>
    rslt = sample(gvn_lst, len_sampl)
  File "/usr/lib/python3.6/random.py", line 320, in sample
    raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative