How to draw vertical lines on a given plot in matplotlib

The standard way to add vertical lines that will cover your entire plot window without you having to specify their actual height is plt.axvline OR You can use many of the keywords available for other plot commands (e.g. color, linestyle, linewidth …). You can pass in keyword arguments ymin and ymax if you like in axes corrdinates (e.g. ymin=0.25, ymax=0.75 will cover the middle half of the … Read more

Converting int to bytes in Python 3

From python 3.2 you can do https://docs.python.org/3/library/stdtypes.html#int.to_bytes Accordingly, x == int_from_bytes(int_to_bytes(x)). Note that the above encoding works only for unsigned (non-negative) integers. For signed integers, the bit length is a bit more tricky to calculate:

What is the meaning of single and double underscore before an object name?

Single Underscore Names, in a class, with a leading underscore are simply to indicate to other programmers that the attribute or method is intended to be private. However, nothing special is done with the name itself. To quote PEP-8: _single_leading_underscore: weak “internal use” indicator. E.g. from M import * does not import objects whose name starts with an … Read more

inserting characters at the start and end of a string

Strings are immutable so you can’t insert characters into an existing string. You have to create a new string. You can use string concatenation to do what you want: Note that you can also create a string with n Ls by using multiplication:

Implementing an efficient queue in Python

As Uri Goren astutely noted above, the Python stdlib already implemented an efficient queue on your fortunate behalf: collections.deque. What Not to Do Avoid reinventing the wheel by hand-rolling your own: Linked list implementation. While doing so reduces the worst-case time complexity of your dequeue() and enqueue() methods to O(1), the collections.deque type already does so. It’s also thread-safe and presumably more space and time … Read more