What is the point of float(‘inf’) in Python?

It acts as an unbounded upper value for comparison. This is useful for finding lowest values for something. for example, calculating path route costs when traversing trees. e.g. Finding the “cheapest” path in a list of options: if you didn’t have float(‘Inf’) available to you, what value would you use for the initial lowest_path_cost? Would 9999999 be enough — float(‘Inf’) removes this … Read more

Single quotes vs. double quotes in Python

I like to use double quotes around strings that are used for interpolation or that are natural language messages, and single quotes for small symbol-like strings, but will break the rules if the strings contain quotes, or if I forget. I use triple double quotes for docstrings and raw string literals for regular expressions even … Read more

What does __contains__ do, what can call __contains__ function

Like all special methods (with “magic names” that begin and end in __), __contains__ is not meant to be called directly (except in very specific cases, such as up=calls to the superclass): rather, such methods are called as part of the operation of built-ins and operators. In the case of __contains__, the operator in question is in — the “containment check” operator. With … Read more

Shuffle DataFrame rows

The idiomatic way to do this with Pandas is to use the .sample method of your dataframe to sample all rows without replacement: The frac keyword argument specifies the fraction of rows to return in the random sample, so frac=1 means return all rows (in random order). Note: If you wish to shuffle your dataframe in-place and reset the index, you could … Read more