What is the difference between ‘/’ and ‘//’ when used for division?

In Python 3.x, 5 / 2 will return 2.5 and 5 // 2 will return 2. The former is floating point division, and the latter is floor division, sometimes also called integer division. In Python 2.2 or later in the 2.x line, there is no difference for integers unless you perform a from __future__ import division, which causes Python 2.x to adopt the 3.x behavior. … Read more

Create empty file using python [duplicate]

There is no way to create a file without opening it There is os.mknod(“newfile.txt”) (but it requires root privileges on OSX). The system call to create a file is actually open() with the O_CREAT flag. So no matter how, you’ll always open the file. So the easiest way to simply create a file without truncating it in case it exists is this: … Read more

Import Error: No module named numpy Anaconda

If you are using Anaconda3 then you should already have numpy installed. There is no reason to use pip. My guess is that the Anaconda distribution is possibly not on your path and you are picking up some other system python. You should run where python (or where python3) to see which one you are using. If the executable … Read more

Difference between except: and except Exception as e:

In the second you can access the attributes of the exception object: But it doesn’t catch BaseException or the system-exiting exceptions SystemExit, KeyboardInterrupt and GeneratorExit: Which a bare except does: See the Built-in Exceptions section of the docs and the Errors and Exceptions section of the tutorial for more info.

log2 in python math module

why doesn’t it exist? >>> [‘log’, ‘log10’, ‘log1p’] I know I can do log(x,2), but log2 is really common, so I’m kind of baffled. Oh, it looks like it’s only defined in C99, not C90, I guess that answers my question. Still seems kind of silly.