Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Understanding the Python sample() function

  • Safa MulaniSafa Mulani
  • November 17, 2020
  • Built-in Methods
Understanding the Python sample() function

Hello, readers! In this article, we will be focusing on the Python sample() function and its importance in the domain of data science.

So, let us get started!


What is the Python sample() method?

Let us first understand the existence of sample() method in the industry of Data science.

While solving problems with respect to the prediction of data, we often come across situations wherein we need to test the algorithm on a handful of data to estimate the accuracy of the algorithm applied.

This is when Python sample() method comes into picture.

The sample() method lets us pick a random sample from the available data for operations. Though, there are lot of techniques to sample the data, sample() method is considered as one of the easiest of its kind.

Python sample() method works will all the types of iterables such as list, tuple, sets, dataframe, etc. It randomly selects data from the iterable through the user defined number of data values.

Let us now understand the structure of the same in the below section.


Syntax of sample() method

Have a look at the below syntax!

Syntax:

sample(iterable, sample_amt)

We need to provide the function with the sample amount that we want the function to randomly pick from the provided iterable or data structure.


1. Python sample() with list

In this section, we have implemented sample() function alongside a Python list and have selected 4 samples out of the data randomly using the function.

Example:

from random import sample 

lst = [10,20,40,30,50,46,89] 
res = sample(lst, 4)
print(res) 

Output:

[20, 89, 40, 46]

2. Python sample() with set

Here, we have created a Python set using alphabets as well as numeric values. Further, we have applied sample() function on the set and selected 4 values at random.

Example:

from random import sample 

set_sample = {1,2,3,4,5,"s","a","f","a"}
res = sample(set_sample, 4)
print(res) 

Output:

['f', 1, 5, 2]

Error and Exceptions with sample() function

While dealing with the sample() function, we can come across a ValueError exception. If we provide the sample_amt as a value that is greater than the total count of data values present in the iterable, this exception is invoked.

Example:

from random import sample 

set_sample = {1,2,3,4,5,"s","a","f","a"}
res = sample(set_sample, 15)
print(res) 

Output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-284724c4b7db> in <module>
      2 
      3 set_sample = {1,2,3,4,5,"s","a","f","a"}
----> 4 res = sample(set_sample, 15)
      5 print(res)

c:\users\hp\appdata\local\programs\python\python36\lib\random.py in sample(self, population, k)
    316         n = len(population)
    317         if not 0 <= k <= n:
--> 318             raise ValueError("Sample larger than population or is negative")
    319         result = [None] * k
    320         setsize = 21        # size of a small set minus size of an empty list

ValueError: Sample larger than population or is negative

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to Python, Stay tuned and till then, Happy Learning! 🙂

Safa Mulani
Safa Mulani

An enthusiast in every forthcoming wonders!

Articles: 189
3 Easy Ways to Print column Names in Python
Previous Post 3 Easy Ways to Print column Names in Python
Next Post How to read MATLAB files in Python (MAT Files in Python)
How to read MATLAB files in Python (MAT Files in Python)

Related Posts

Print Function Python: Arguments and Output

Print Function Python: Arguments and Output

  • November 28, 2026
How to Use Input Function in Python

How to Use Input Function in Python

  • November 28, 2026
How to Open Files in Python

How to Open Files in Python

  • November 23, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.