What does random.sample() method in python do?

According to documentation: random.sample(population, k) Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement. Basically, it picks k unique random elements, a sample, from a sequence: random.sample works also directly from a range: In addition to sequences, random.sample works with sets too: However, random.sample doesn’t … Read more

What is the maximum recursion depth in Python, and how to increase it?

It is a guard against a stack overflow, yes. Python (or rather, the CPython implementation) doesn’t optimize tail recursion, and unbridled recursion causes stack overflows. You can check the recursion limit with sys.getrecursionlimit: and change the recursion limit with sys.setrecursionlimit: but doing so is dangerous — the standard limit is a little conservative, but Python … Read more

Remove all whitespace in a string

If you want to remove leading and ending spaces, use str.strip(): If you want to remove all space characters, use str.replace(): (NB this only removes the “normal” ASCII space character ‘ ‘ U+0020 but not any other whitespace) If you want to remove duplicated spaces, use str.split():

Saving and loading objects and using pickle

I´m trying to save and load objects using pickle module.First I declare my objects: After that I open a file called ‘Fruits.obj'(previously I created a new .txt file and I renamed ‘Fruits.obj’): After do this I close my session and I began a new one and I put the next (trying to access to the … Read more