Exit while loop in Python

The while loop will match the condition only when the control returns back to it, i.e when the for loops are executed completely. So, that’s why your program doesn’t exits immediately even though the condition was met. But, in case the condition was not met for any values of a,b,c then your code will end up in an infinite loop. You … Read more

What is the difference between np.mean and tf.reduce_mean?

The functionality of numpy.mean and tensorflow.reduce_mean are the same. They do the same thing. From the documentation, for numpy and tensorflow, you can see that. Lets look at an example, Output Here you can see that when axis(numpy) or reduction_indices(tensorflow) is 1, it computes mean across (3,4) and (5,6) and (6,7), so 1 defines across which axis the mean is computed. When it is 0, the … Read more

memory error in python

This one here: seems to be very inefficient and expensive for large strings. Better do A buffer object keeps a reference to the original string and start and length attributes. This way, no unnecessary duplication of data occurs. A string of length l has l*l/2 sub strings of average length l/2, so the memory consumption would roughly be l*l*l/4. With a … Read more

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