[Tutor] A couple newbie questions about Python
Steven D'Aprano
steve at pearwood.info
Thu Jun 12 05:57:06 CEST 2014
Hi Deb,
My responses below, interleaved with your questions.
On Wed, Jun 11, 2014 at 12:46:11PM -0800, Deb Wyatt wrote:
> Hi. Everywhere I have read, the 'standard practice' for indentation
> is 4 spaces, but I am running into 2 space indentation in a lot of
> tutorials and such. Should I keep with the 4 spaces, or does it even
> matter, as long as it is consistent?
Four spaces is common, and recommended by "PEP 8", which describes the
coding styles for the Python standard library:
https://www.python.org/dev/peps/pep-0008
But it's not compulsory (except for contributions to the standard
library). There are good reasons for sticking to four spaces, but eight
spaces or a single tab are also common. In my opinion, two spaces is too
little, and one space is right out.
But as far as the Python interpreter is concerned, it doesn't care, so
long as you're consistent. (However, other people reading your code may
care.)
> I just recently became aware of the inaccuracy of calculations using
> floats and I am concerned about that.
Floating point mathematics is tricky. What you're seeing is not a bug in
Python, but a general problem with floating point calculations in
general. Regardless of whether you use Python, or C, or Java, or some
other programming language, you have to face the same issues that
calculations with floats are not always accurate compared to what you
expect from pure mathematics.
(Think about your calculator: how often do you get a number like
3.0000001 instead of 3, or 0.4999999 instead of 0.5?)
You can read up on some of the issues here:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
although it's quite technical. Some less technical discussions can be
found here:
http://randomascii.wordpress.com/2012/04/05/floating-point-complexities/
http://support.microsoft.com/kb/42980
but however you look at it, it's complicated, and inherent to the
problem, not the fault of Python.
--
Steven
More information about the Tutor
mailing list