[Tutor] Hello, and a newbie question

2013-04-16 Thread Andy McKenzie
Hey folks.

I'm just starting to pick up Python, and I'd like to avoid some of the
mistakes I made in the past.  To elaborate on that, my primary
programming/scripting experience is PHP, with a little bit of Perl thrown
in.  Like so many people who write in PHP, I was entirely self-taught, and
would be the first to admit that a lot of what I've written is, well...
wrong.  It works, but it's sloppy and inefficient, because there were
standard constructions and solutions I just didn't know about.  I'd like to
avoid that with Python.

So:  my first two questions to the list.

1) Python 2.7 or 3.x?  I know I'm going to want to do some work with NLTK
(which appears to only have an alpha version out for Python 3), but I've
just gone through the hassle of dealing with an upgrade from PHP 4 to 5.3,
and I'd rather not start learning something that's already obsolete.  Any
words of advice?

2) Best practices.  I have the WROX Press Beginning Python book, which
targets Python 2.  Clearly that's of only limited value if I'm going to go
with Python 3, but it looks like it's at least going to be a good overview.
 But some of the stuff they do seems to be fairly personalized, rather than
trying to follow standards.  Should I just start out with the tutorial from
docs.python.org?  I would assume that that would start putting me in the
right habits from the beginning... is that accurate, or is there a better
way to go?

Thanks in advance,
  Andy McKenzie
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello, and a newbie question

2013-04-16 Thread Alan Gauld

On 16/04/13 16:58, Andy McKenzie wrote:


1) Python 2.7 or 3.x?  I know I'm going to want to do some work with
NLTK (which appears to only have an alpha version out for Python 3), but
I've just gone through the hassle of dealing with an upgrade from PHP 4
to 5.3, and I'd rather not start learning something that's already
obsolete.  Any words of advice?


Upgrading from P2 to P3 is not too onerous but there will be some 
changes to make at some po9nt.
However P2 is the one to go for if you want to do anything industrial 
just now because not all the 3rd party libraries (like NLTK) are fully 
ported to v3 yet, including some pretty significant ones.


OTOH If you are only experimenting/learning then going with P3 will 
avoid any relearning in the future.



2) Best practices.  I have the WROX Press Beginning Python book,


Sorry, I've never even seen that one so can't comment...


?  I would assume that that would start putting
me in the right habits from the beginning... is that accurate,


Yes, for existing programmers new to Python the official tutor is nearly 
always the best place to start. You can fill in the gaps elsewhere 
later. And the tutor is pretty short and fast paced, you can just about 
get through it all in an afternoon - certainly in a day.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Hello, and a newbie question

2013-04-16 Thread leam hall
Python 2.x if you're working on existing servers like RHEL. If you're
having fun on a desktop, Python 3.

Free on-line college level class just started:
https://class.coursera.org/interactivepython-002/class/index

BTW, PHP ROCKS!  :P

Leam
  -- Also a PHP programmer


-- 
Mind on a Mission 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] oops - resending as plain text

2013-04-16 Thread Jim Mooney
I accidentally sent as HTML so this is a resend in case that choked
the mailing prog ;')

I was doing a simple training prog to figure monetary change, and
wanted to avoid computer inaccuracy by using only two-decimal input
and not using division or mod where it would cause error. Yet, on a
simple subtraction I got a decimal error instead of a two decimal
result, as per below. What gives?

cost = float(input('How much did the item cost?: '))
paid = float(input('How much did the customer give you?: '))
change = paid - cost

#using 22.89 as cost and 248.76 as paid

twenties = int(change / 20)
if twenties != 0:
  twentiesAmount = 20 * twenties
  change = change - twentiesAmount
  #change is 5.8745, not 5.87 - how did I get this decimal
error when simply subtracting an integer from what  should be a
  #two-decimal amount?
  print(twenties, ' twenties')
  print(change)

#and so forth for the rest of the prog

Jim Mooney
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How did this decimal error pop up?

2013-04-16 Thread Jim Mooney
I was doing a simple training prog to figure change, and wanted to avoid
computer inaccuracy by using only two-decimal input and not using division
or mod where it would cause error. Yet, on a simple subtraction I got a
decimal error instead of a two decimal result, as per below. What gives?

cost = float(input('How much did the item cost?: '))
paid = float(input('How much did the customer give you?: '))
change = paid - cost

*#using 22.89 as cost and 248.76 as paid*

twenties = int(change / 20)
if twenties != 0:
  twentiesAmount = 20 * twenties
  change = change - twentiesAmount
  *#change is 5.8745, not 5.87 - how did I get this decimal
error when simply subtracting an integer from what  should be a
  #two-decimal amount?
*  print(twenties, ' twenties')
  print(change)

#and so forth for the rest of the change

-- 
*Jim Mooney

If you shoot a child you're a bad guy. If you splatter forty children
across a wall with a bomb, you're a heroic, manly Top Gun with gleaming Tom
Cruise teeth. The moral is you'll get laid more if you snuff a lot of
children than if you only snuff a few.*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] oops - resending as plain text

2013-04-16 Thread Joel Goldstick
On Tue, Apr 16, 2013 at 1:48 PM, Jim Mooney wrote:

> I accidentally sent as HTML so this is a resend in case that choked
> the mailing prog ;')
>
> I was doing a simple training prog to figure monetary change, and
> wanted to avoid computer inaccuracy by using only two-decimal input
> and not using division or mod where it would cause error. Yet, on a
> simple subtraction I got a decimal error instead of a two decimal
> result, as per below. What gives?
>
> cost = float(input('How much did the item cost?: '))
> paid = float(input('How much did the customer give you?: '))
> change = paid - cost
>
> #using 22.89 as cost and 248.76 as paid
>
> twenties = int(change / 20)
> if twenties != 0:
>   twentiesAmount = 20 * twenties
>   change = change - twentiesAmount
>   #change is 5.8745, not 5.87 - how did I get this decimal
> error when simply subtracting an integer from what  should be a
>   #two-decimal amount?
>

because binary computers convert decimal numbers to binary numbers.  There
is often a rounding error.

>   print(twenties, ' twenties')
>   print(change)
>
> #and so forth for the rest of the prog
>
> Jim Mooney
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Joel Goldstick
http://joelgoldstick.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] oops - resending as plain text

2013-04-16 Thread Don Jennings

On Apr 16, 2013, at 1:48 PM, Jim Mooney wrote:

> I accidentally sent as HTML so this is a resend in case that choked
> the mailing prog ;')
> 
> I was doing a simple training prog to figure monetary change, and
> wanted to avoid computer inaccuracy by using only two-decimal input
> and not using division or mod where it would cause error. Yet, on a
> simple subtraction I got a decimal error instead of a two decimal
> result, as per below. What gives?
> 
> cost = float(input('How much did the item cost?: '))
> paid = float(input('How much did the customer give you?: '))
> change = paid - cost
> 
> #using 22.89 as cost and 248.76 as paid
> 
> twenties = int(change / 20)
> if twenties != 0:
>  twentiesAmount = 20 * twenties
>  change = change - twentiesAmount
>  #change is 5.8745, not 5.87 - how did I get this decimal
> error when simply subtracting an integer from what  should be a

Now that Joel Goldstick has pointed out the reason, you may wonder what to do 
now. Answer? Use the decimal module:

http://docs.python.org/2/library/decimal.html

Although, you might prefer Doug Hellmann's introduction:

http://pymotw.com/2/decimal/

Take care,
Don

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] oops - resending as plain text

2013-04-16 Thread Sander Sweers
On 04/16/2013 07:48 PM, Jim Mooney wrote:
> I accidentally sent as HTML so this is a resend in case that choked
> the mailing prog ;')
> 
> I was doing a simple training prog to figure monetary change, and
> wanted to avoid computer inaccuracy by using only two-decimal input
> and not using division or mod where it would cause error. Yet, on a
> simple subtraction I got a decimal error instead of a two decimal
> result, as per below. What gives?

Floats can not be represented accurately in binary and will have small
rounding errors. See
https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems.

> cost = float(input('How much did the item cost?: '))
> paid = float(input('How much did the customer give you?: '))
> change = paid - cost
> 
> #using 22.89 as cost and 248.76 as paid
> 
> twenties = int(change / 20)
> if twenties != 0:
>   twentiesAmount = 20 * twenties
>   change = change - twentiesAmount
>   #change is 5.8745, not 5.87 - how did I get this decimal
> error when simply subtracting an integer from what  should be a
>   #two-decimal amount?
>   print(twenties, ' twenties')
>   print(change)
> 
> #and so forth for the rest of the prog

You can use string formatting to show as much precision you want. Example:

>>> n = 5.8745
>>> print '%.2f' % n
5.87
>>> print '%.4f' % n
5.8700

Or use round(), example:

>>> round(n,2)
5.87

In your case I would just use string formatting to hide the rounding error.

Greets
~sander
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello, and a newbie question

2013-04-16 Thread Dave Angel

On 04/16/2013 11:58 AM, Andy McKenzie wrote:

Hey folks.

I'm just starting to pick up Python, and I'd like to avoid some of the
mistakes I made in the past.  To elaborate on that, my primary
programming/scripting experience is PHP, with a little bit of Perl thrown
in.  Like so many people who write in PHP, I was entirely self-taught, and
would be the first to admit that a lot of what I've written is, well...
wrong.  It works, but it's sloppy and inefficient, because there were
standard constructions and solutions I just didn't know about.  I'd like to
avoid that with Python.



Welcome to the mailing list.  I expect you'll find Python a much cleaner 
language than the other two, though php has some definite convenience 
for its particular niche.





So:  my first two questions to the list.

1) Python 2.7 or 3.x?  I know I'm going to want to do some work with NLTK
(which appears to only have an alpha version out for Python 3), but I've
just gone through the hassle of dealing with an upgrade from PHP 4 to 5.3,
and I'd rather not start learning something that's already obsolete.  Any
words of advice?



If you have to use a library that's not available yet for 3.x, then you 
need to use 2.x  on the other hand, if you're learning now, maybe that 
library will be available by the time you actually need it.


For most people, I'd advise against trying to use a tutorial that 
targets a different version than you're running.  If you get frustrated 
quickly, you can get bogged down by the differences when you're just 
copying an exact program out of some book.


Python 3 in particular has spent some substantial effort cleaning up the 
warts, the biggest one being Unicode.  For beginning programmers using 
only ASCII, probably the main thing that'll bog you down is that print() 
is now a function, rather than a statement, so you need parentheses. 
But once you get used to seeing syntax error, you quickly get the hang 
of it.  And once you do, the function is much nicer.




2) Best practices.  I have the WROX Press Beginning Python book, which
targets Python 2.  Clearly that's of only limited value if I'm going to go
with Python 3, but it looks like it's at least going to be a good overview.
  But some of the stuff they do seems to be fairly personalized, rather than
trying to follow standards.  Should I just start out with the tutorial from
docs.python.org?  I would assume that that would start putting me in the
right habits from the beginning... is that accurate, or is there a better
way to go?

Thanks in advance,
   Andy McKenzie


I'd start with the python.org tutorial for the version you're trying to 
learn.  Get serious about trying everything, and don't try to absorb it 
all in one sitting, even though it can be done.


And use a text editor that helps you indent, or even that colorizes your 
code.  And when you just want to try things, use the interpreter 
directly.  It's amazing what you can learn directly from it.  You can 
ask the interpreter lots of questions about an object:


   help(obj)
   dir(obj)
   print( type(obj) )
   print( repr(obj) )

And don't forget to post here when you seem to be stuck.  Sometimes a 
well placed comment beats days of struggling.  When you do get an 
exception you don't understand, paste the whole thing, as well as the 
code you were trying.


Best of luck.



--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] oops - resending as plain text

2013-04-16 Thread Dave Angel

On 04/16/2013 01:48 PM, Jim Mooney wrote:

I accidentally sent as HTML so this is a resend in case that choked
the mailing prog ;')

I was doing a simple training prog to figure monetary change, and
wanted to avoid computer inaccuracy by using only two-decimal input
and not using division or mod where it would cause error. Yet, on a
simple subtraction I got a decimal error instead of a two decimal
result, as per below. What gives?

cost = float(input('How much did the item cost?: '))
paid = float(input('How much did the customer give you?: '))
change = paid - cost

#using 22.89 as cost and 248.76 as paid

twenties = int(change / 20)
if twenties != 0:
   twentiesAmount = 20 * twenties
   change = change - twentiesAmount
   #change is 5.8745, not 5.87 - how did I get this decimal
error when simply subtracting an integer from what  should be a
   #two-decimal amount?



The other responses have been good.  But let me point some things out. 
Binary floating point has been around as long as I've been in computers, 
which was in 1967.  I saw a detailed rant about avoiding errors in 
manipulating floats, in that year.


The fact is that rounding errors can occur in lots of surprising places. 
 For example, if you're using pencil and paper, and you divide 7 by 3, 
you get 2.33   and at some point you'll stop writing the 3's.  if 
you then multiply by 3, you get  6.9  not 7.  Now if you're doing it 
by hand, you just adjust it without thinking much.  But a computer is a 
very literal thing, and fudging numbers isn't necessarily a good idea.


In the code above you divided a number by 20, then multiplied by 20 
again.  That's an operation that happens to come out even in decimal, 
because 20 is a factor of 100, the base squared.  But just like 1/3 is a 
problem in a decimal system, so 1/20 is a problem in a binary one.


The chief advantage of decimal is NOT that it's more accurate, but that 
it gets the numbers wrong when YOU expect it, not by some other, more 
subtle rule.  And the second advantage is that there are fewer places 
where the numbers need to be converted, and each conversion might 
produce an error.


It happens that binary floats, as used on Intel hardware, are accurate 
for integers up to a fairly large value.  So some people will represent 
money in pennies, adjusting only when it's time to print.  Others will 
simply use integers (which have no practical size limit on python 3.x) 
for the same purpose.  That's frequently necessary when doing accounting 
or banking, since many transactions are rounded or truncated to the 
nearest penny at each step, according to some standardized rules, rather 
than keeping the numbers more "accurate."


Bottom line, you need to understand enough about what's going on to 
avoid getting burned.


And in case it wasn't obvious, it's not unique to Python.  It's 
potentially  a problem in any environment that doesn't have infinite 
precision.


--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello, and a newbie question

2013-04-16 Thread Andy McKenzie
On Tue, Apr 16, 2013 at 4:18 PM, Dave Angel  wrote:

> On 04/16/2013 11:58 AM, Andy McKenzie wrote:
>
>> Hey folks.
>>
>> I'm just starting to pick up Python, and I'd like to avoid some of the
>> mistakes I made in the past.  To elaborate on that, my primary
>> programming/scripting experience is PHP, with a little bit of Perl thrown
>> in.  Like so many people who write in PHP, I was entirely self-taught, and
>> would be the first to admit that a lot of what I've written is, well...
>> wrong.  It works, but it's sloppy and inefficient, because there were
>> standard constructions and solutions I just didn't know about.  I'd like
>> to
>> avoid that with Python.
>>
>>
> Welcome to the mailing list.  I expect you'll find Python a much cleaner
> language than the other two, though php has some definite convenience for
> its particular niche.
>
>
>
>
>  So:  my first two questions to the list.
>>
>> 1) Python 2.7 or 3.x?  I know I'm going to want to do some work with NLTK
>> (which appears to only have an alpha version out for Python 3), but I've
>> just gone through the hassle of dealing with an upgrade from PHP 4 to 5.3,
>> and I'd rather not start learning something that's already obsolete.  Any
>> words of advice?
>>
>>
> If you have to use a library that's not available yet for 3.x, then you
> need to use 2.x  on the other hand, if you're learning now, maybe that
> library will be available by the time you actually need it.
>
> For most people, I'd advise against trying to use a tutorial that targets
> a different version than you're running.  If you get frustrated quickly,
> you can get bogged down by the differences when you're just copying an
> exact program out of some book.
>
> Python 3 in particular has spent some substantial effort cleaning up the
> warts, the biggest one being Unicode.  For beginning programmers using only
> ASCII, probably the main thing that'll bog you down is that print() is now
> a function, rather than a statement, so you need parentheses. But once you
> get used to seeing syntax error, you quickly get the hang of it.  And once
> you do, the function is much nicer.
>
>
>
>  2) Best practices.  I have the WROX Press Beginning Python book, which
>> targets Python 2.  Clearly that's of only limited value if I'm going to go
>> with Python 3, but it looks like it's at least going to be a good
>> overview.
>>   But some of the stuff they do seems to be fairly personalized, rather
>> than
>> trying to follow standards.  Should I just start out with the tutorial
>> from
>> docs.python.org?  I would assume that that would start putting me in the
>> right habits from the beginning... is that accurate, or is there a better
>> way to go?
>>
>> Thanks in advance,
>>Andy McKenzie
>>
>
> I'd start with the python.org tutorial for the version you're trying to
> learn.  Get serious about trying everything, and don't try to absorb it all
> in one sitting, even though it can be done.
>
> And use a text editor that helps you indent, or even that colorizes your
> code.  And when you just want to try things, use the interpreter directly.
>  It's amazing what you can learn directly from it.  You can ask the
> interpreter lots of questions about an object:
>
>help(obj)
>dir(obj)
>print( type(obj) )
>print( repr(obj) )
>
> And don't forget to post here when you seem to be stuck.  Sometimes a well
> placed comment beats days of struggling.  When you do get an exception you
> don't understand, paste the whole thing, as well as the code you were
> trying.
>
> Best of luck.
>
>
>
>
Thanks for the advice, folks.  Given that it looks like the biggest changes
are unicode handling (which I'm not going to need any time soon) and the
way the print function works, I decided to stick with 2.7.  I'm an IT guy,
though unemployed at the moment, and it occurred to me that "I'm familiar
with Python, but not the version your entire established codebase is in"
wasn't a great thing to have on a resume.

Since it looks like the new formatting for print -- that is, print("Print
this stuff!") -- works fine in 2.7, I'm just getting myself used to doing
that from the beginning.

I went through the first four or five sections of the tutorial this
afternoon, with a few side trips into things that got me interested, and I
figure I'll do at least one more section after dinner.  I did find it
interesting that one of the first things I wanted to know turned out to be
an extremely common question:  "What's the Python equivalent to print_r()
from PHP?"  If any of you are familiar with PHP (I know at least a couple
of you seemed to be), you'll know that pprint() (which seems to be the most
common answer) isn't actually very close.  Its output isn't nearly as
readable.

For instance:  output of running print_r on a very short dictionary from
PHP:

Array
(
[key3] => thing3
[key2] => thing2
[key1] => thing1
)

And running pprint on the same dict in Python:

{'key1': 'thing1', 'key2': 'thing2', 'key3': 't

Re: [Tutor] Hello, and a newbie question

2013-04-16 Thread Dave Angel

On 04/16/2013 05:20 PM, Andy McKenzie wrote:









Thanks for the advice, folks.  Given that it looks like the biggest changes
are unicode handling (which I'm not going to need any time soon) and the
way the print function works, I decided to stick with 2.7.  I'm an IT guy,
though unemployed at the moment, and it occurred to me that "I'm familiar
with Python, but not the version your entire established codebase is in"
wasn't a great thing to have on a resume.

Since it looks like the new formatting for print -- that is, print("Print
this stuff!") -- works fine in 2.7, I'm just getting myself used to doing
that from the beginning.



The degenerate print, where you're printing exactly one thing, works the 
same.  But if you have two things to print, putting parens around them 
in Python 2.x will cause a tuple to be printed, rather than printing the 
two with a space between.


>>> print(3,5)  -- version 2.x
(3, 5)

>>> print(3,5)  -- version 3.x
3 5

To get 3.x functionality, you'd want to use
from __future__ import print_function

and I do not think that works in 2.6 or older versions.  It also can be 
awkward even in 2.7 if you're mixing existing code with new print functions.




--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello, and a newbie question

2013-04-16 Thread Andy McKenzie
On Tue, Apr 16, 2013 at 5:31 PM, Dave Angel  wrote:

> On 04/16/2013 05:20 PM, Andy McKenzie wrote:
>
>>
>>
>>  
>
>
>>>
>>>  Thanks for the advice, folks.  Given that it looks like the biggest
>> changes
>> are unicode handling (which I'm not going to need any time soon) and the
>> way the print function works, I decided to stick with 2.7.  I'm an IT guy,
>> though unemployed at the moment, and it occurred to me that "I'm familiar
>> with Python, but not the version your entire established codebase is in"
>> wasn't a great thing to have on a resume.
>>
>> Since it looks like the new formatting for print -- that is, print("Print
>> this stuff!") -- works fine in 2.7, I'm just getting myself used to doing
>> that from the beginning.
>>
>>
> The degenerate print, where you're printing exactly one thing, works the
> same.  But if you have two things to print, putting parens around them in
> Python 2.x will cause a tuple to be printed, rather than printing the two
> with a space between.
>
> >>> print(3,5)  -- version 2.x
> (3, 5)
>
> >>> print(3,5)  -- version 3.x
> 3 5
>
> To get 3.x functionality, you'd want to use
> from __future__ import print_function
>
> and I do not think that works in 2.6 or older versions.  It also can be
> awkward even in 2.7 if you're mixing existing code with new print functions.
>
>
That's good to know, since I hadn't run into it yet.

So am I correct in understanding that I can just put the from __future__
import print_function in each new 2.7 script, and get identical
functionality to what happens in 3.x?  Or do I need to do that system-wide
somehow?

-Andy
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 110, Issue 69

2013-04-16 Thread Jim Mooney
> Now that Joel Goldstick has pointed out the reason, you may wonder what to do 
> now. Answer? Use the decimal module:
>
> http://docs.python.org/2/library/decimal.html
>
> Although, you might prefer Doug Hellmann's introduction:
>
> http://pymotw.com/2/decimal/

Thanks. I was about to ask that but you beat me to it. I'll try
Hellmann. The manual is just a tad wordy and it's hard to find what
you want.

Jim
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] oops - resending as plain text

2013-04-16 Thread Jim Mooney
Further question. If I round the input right at the beginning,
round(paid,2) does that mean  I still have the original error from
using .76 even before math, or does the rounding kill it? I would
guess not if it's binary, although Python must have a way to handle
money amounts. I'm only on Chapter 2 ;')

I assume Python has some automatic way to filter input, so that if
someone entered three decimals instead of two for a money amount, they
could get a wrist slap. Can you direct me to that functionality?
Thanks.

Jim

On 16 April 2013 11:55, Sander Sweers  wrote:
> On 04/16/2013 07:48 PM, Jim Mooney wrote:
>> I accidentally sent as HTML so this is a resend in case that choked
>> the mailing prog ;')
>>
>> I was doing a simple training prog to figure monetary change, and
>> wanted to avoid computer inaccuracy by using only two-decimal input
>> and not using division or mod where it would cause error. Yet, on a
>> simple subtraction I got a decimal error instead of a two decimal
>> result, as per below. What gives?
>
> Floats can not be represented accurately in binary and will have small
> rounding errors. See
> https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems.
>
>> cost = float(input('How much did the item cost?: '))
>> paid = float(input('How much did the customer give you?: '))
>> change = paid - cost
>>
>> #using 22.89 as cost and 248.76 as paid
>>
>> twenties = int(change / 20)
>> if twenties != 0:
>>   twentiesAmount = 20 * twenties
>>   change = change - twentiesAmount
>>   #change is 5.8745, not 5.87 - how did I get this decimal
>> error when simply subtracting an integer from what  should be a
>>   #two-decimal amount?
>>   print(twenties, ' twenties')
>>   print(change)
>>
>> #and so forth for the rest of the prog
>
> You can use string formatting to show as much precision you want. Example:
>
 n = 5.8745
 print '%.2f' % n
> 5.87
 print '%.4f' % n
> 5.8700
>
> Or use round(), example:
>
 round(n,2)
> 5.87
>
> In your case I would just use string formatting to hide the rounding error.
>
> Greets
> ~sander



-- 
Jim Mooney

If you shoot a child you're a bad guy. If you splatter forty children
across a wall with a bomb, you're a heroic, manly Top Gun with
gleaming Tom Cruise teeth. The moral is you'll get laid more if you
snuff a lot of children than if you only snuff a few.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] oops - resending as plain text

2013-04-16 Thread Dave Angel

On 04/16/2013 05:46 PM, Jim Mooney wrote:

Further question. If I round the input right at the beginning,
round(paid,2) does that mean  I still have the original error from
using .76 even before math, or does the rounding kill it? I would
guess not if it's binary,


You guess right.  round() function is only theoretical, when applied to 
floats.  It can be useful when combined with formatting, as suggested 
elsewhere in the thread.  But even if .76 prints out "correctly," you 
have no assurance that adding two such apparently-exact figures will 
give one that will also print out easily.



although Python must have a way to handle
money amounts. I'm only on Chapter 2 ;')

I assume Python has some automatic way to filter input, so that if
someone entered three decimals instead of two for a money amount, they
could get a wrist slap. Can you direct me to that functionality?
Thanks.



There's no "money type" in Python, and if you want your user to get 
slapped, you have to write your own.  Normally, you take input in the 
form of text, validate it, then convert to int, float, Decimal, or 
whatever.  If the conversion itself will catch the errors, then you can 
just use try/catch to make such errors more polite to your users. 
That's the case if they enter  "charlie" when you're asking for a salary 
value.  But if they were to put  7.500 when the value is supposed to be 
in dollars and cents, then by the time it's turned into a float or 
decimal, the extra zero is long gone.


--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello, and a newbie question

2013-04-16 Thread Dave Angel

On 04/16/2013 05:47 PM, Andy McKenzie wrote:

On Tue, Apr 16, 2013 at 5:31 PM, Dave Angel  wrote:




To get 3.x functionality, you'd want to use
 from __future__ import print_function

and I do not think that works in 2.6 or older versions.  It also can be
awkward even in 2.7 if you're mixing existing code with new print functions.



That's good to know, since I hadn't run into it yet.

So am I correct in understanding that I can just put the from __future__
import print_function in each new 2.7 script, and get identical
functionality to what happens in 3.x?  Or do I need to do that system-wide
somehow?



Someone else may know if "identical" has some exceptions.  But as for 
where to put it, you'd need it for any module (including your own 
script) which is going to use the newer print() function.


And the nice thing is that the from-future directive is ignored in 3.x, 
so you don't have to remove it when you do progress.


--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How did this decimal error pop up?

2013-04-16 Thread Hugo Arts
On Tue, Apr 16, 2013 at 7:43 PM, Jim Mooney wrote:

> I was doing a simple training prog to figure change, and wanted to avoid
> computer inaccuracy by using only two-decimal input and not using division
> or mod where it would cause error. Yet, on a simple subtraction I got a
> decimal error instead of a two decimal result, as per below. What gives?
>
>
Your assumption that using only two-decimal input and no division/modulo
would avoid computer inaccuracy is wrong. In fact, for some values floating
point numbers don't even have 1 decimal accuracy (all examples done in
python 2.7.3):

 >>> 0.1 + 0.2
0.30004

This is a limitation of the way floating point numbers are represented in
hardware (some python versions may actually print 0.3 here, but the number
stored in memory is *not* 0.3. It can't be). floating point accuracy isn't
as simple as "accurate to 5 decimal places, always." Some numbers simply
*can not* be represented as a floating point value. A simple example of
this is the number 0.1, it has no exact floating point representation. Note
that python appears to have no problem storing the value 0.1:

>>> a = 0.1
>>> a
0.1

But this is just because python actually rounds numbers when it is
displaying them. We can show that there are still errors by adding the
errors up to make them visible:

>>> i = 0
>>> for _ in range(1000):
i += 0.1

 >>> i
99.99986

That should, by all the laws of math, be 100. But it isn't. Because the
computer can't store 0.1 in a float, so it stores the closest possible
number it can instead. The value stored in a above is not actually 0.1,
it's closer to:

0.155511151231257827021181583404541015625

The precise value kind of depends on what hardware you have, but it can
*never* be 0.1 exactly. The format that floating point numbers are stored
in simply cannot represent the value 0.1. The reason for this is kind of
complicated, it has to do with how the number is stored in hardware. A good
analogy for base 10 is the number 1/3. To write that number down in a
decimal notation you'd need an infinite amount of space: 0.333. and
so on. In floating point, 0.1, along with a ton of other numbers, are the
same way: you'd need an infinite amount of space to store them.

To store decimals with perfect accuracy, you have 2 options. The most basic
is what is generally called "fixed point" arithmetic. You decide beforehand
on the amount of accuracy you would like to have, say 2 decimals. Then you
simply multiply all numbers by 10^decimals_of_accuracy (in this case, 100)
and store them in a regular int. So 10.43 would become 1043. Then when
you're displaying a number, just put the decimal point back in. This is
pretty simple, gives you perfect (though fixed) accuracy, but it's also
kind of tedious.

The other option, and probably the simplest to use, is python's built in
decimal module, which provides a special class that implements perfectly
accurate decimal calculations. Why don't we use it for all applications,
you might ask? Well, it is extremely slow when compared to floats, and
usually float accuracy is good enough for people that they don't care about
the inaccuracies. Only packages that absolutely need accuracy use decimal,
like accounting software.

The documentation for the decimal module is here:
http://docs.python.org/2/library/decimal.html
everything a computer scientist should know about floating point is here:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

HTH,
Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] oops - resending as plain text

2013-04-16 Thread eryksun
On Tue, Apr 16, 2013 at 6:09 PM, Dave Angel  wrote:
> If the conversion itself will catch the errors, then you can just use
> try/catch to make such errors more polite to your users. That's the case if
> they enter  "charlie" when you're asking for a salary value.  But if they
> were to put  7.500 when the value is supposed to be in dollars and cents,
> then by the time it's turned into a float or decimal, the extra zero is long
> gone.

With Decimal it's not really 'long gone'. It affects the number of
significant digits retained in arithmetic operations:

>>> from decimal import Decimal
>>> context = decimal.getcontext()

>>> Decimal('7.500') + Decimal('0.50')
Decimal('8.000')

You could look specifically at the _exp attribute, or quantize with a
trap for inexact rounding:

>>> Decimal('7.500')._exp
-3

def round_money(value):
with decimal.localcontext() as ctx:
ctx.traps[decimal.Inexact] = 1
return Decimal(value).quantize(Decimal('1.00'))

>>> try: round_money('7.500')
... except decimal.Inexact: print 'ack!'
...
Decimal('7.50')

>>> try: round_money('7.501')
... except decimal.Inexact: print 'ack!'
...
ack!

>>> try: round_money('charlie')
... except decimal.InvalidOperation as e: print 'ack!'
...
ack!

Please don't print a useless error message like 'ack'; it was just a
silly example. Handle the error within the logical context of your
application.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How did this decimal error pop up?

2013-04-16 Thread Dave Angel

On 04/16/2013 06:10 PM, Hugo Arts wrote:


   
The other option, and probably the simplest to use, is python's built in
decimal module, which provides a special class that implements perfectly
accurate decimal calculations. Why don't we use it for all applications,
you might ask? Well, it is extremely slow when compared to floats, and


I've been told (here) that starting with about Python 3.3, the decimal 
package is nearly as fast as float, for typical use.  The underlying 
processor logic is much slower, but it gets swamped by Python's overhead 
manipulating objects and searching dicts.


By nearly as fast, I'm thinking like 30%, rather than the previous 
1000%.  Or some such.


--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How did this decimal error pop up?

2013-04-16 Thread eryksun
On Tue, Apr 16, 2013 at 6:10 PM, Hugo Arts  wrote:
 i = 0
 for _ in range(1000):
> i += 0.1
>
 i
> 99.99986
>
> That should, by all the laws of math, be 100. But it isn't. Because the
> computer can't store 0.1 in a float, so it stores the closest possible
> number it can instead. The value stored in a above is not actually 0.1, it's
> closer to:
>
> 0.155511151231257827021181583404541015625

Now you have to explain how the summation ends up as less than 100. ;)
 That really gets to the heart of error introduced by intermediate
rounding with fixed-precision arithmetic. This is a more important
point than the fact that 0.1 doesn't have an exact representation in
binary floating point. Python's floats are limited to hardware
precision, which is 53 bits or floor(53*log10(2)) == 15 decimal
digits. There's no option to extend the precision arbitrarily as is
possible with the Decimal type.

BTW, did you know Decimal got a C implementation in 3.3? It's a lot
faster than the pure Python implementation in previous versions, but
still not as fast as hardware floats.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello, and a newbie question

2013-04-16 Thread Alan Gauld

On 16/04/13 22:20, Andy McKenzie wrote:


For instance:  output of running print_r on a very short dictionary from
PHP:

Array
(
 [key3] => thing3
 [key2] => thing2
 [key1] => thing1
)

And running pprint on the same dict in Python:

{'key1': 'thing1', 'key2': 'thing2', 'key3': 'thing3'}


I finally decided that a good project would be building a quick function
that recreates the print_r function, and I got that working.  New
function output, in Python:

(
 [key3] => thing3
 [key2] => thing2
 [key1] => thing1
)



You should probably use {} instead of () as the delimiter to indicate 
that the thing is a dictionary and not a tuple. but otherwise that's a 
good starter project.



--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello, and a newbie question

2013-04-16 Thread eryksun
On Tue, Apr 16, 2013 at 6:17 PM, Dave Angel  wrote:
> Someone else may know if "identical" has some exceptions.  But as for where
> to put it, you'd need it for any module (including your own script) which is
> going to use the newer print() function.

I think any differences will result from the I/O system redesign in
3.x. In 2.x sys.stdout is still the old "file" type, which is
basically a wrapper around CRT FILE streams, while in 3.x it's a
TextIOWrapper wrapping a BufferedWriter wrapping a FileIO object.

Off the top of my head I don't have a clear example where it matters.
I vaguely recall having an issue with the default buffer flushing not
being the same. I think I was simulating scrolling text by printing a
carriage return ('\r') to overwrite a line. IRRC, in the end I opted
to directly use sys.stdout.write() and sys.stdout.flush().

Since __future__ imports are compiler directive, you have to include
them at the top of every module. "print_function" works in 2.6+. If
you just want the function under a different name, you can grab it
from the __builtin__ module:

>>> import __builtin__
>>> printf = getattr(__builtin__, 'print')
>>> printf('spam')
spam
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello, and a newbie question

2013-04-16 Thread Andy McKenzie
On Tue, Apr 16, 2013 at 7:39 PM, Alan Gauld wrote:

> On 16/04/13 22:20, Andy McKenzie wrote:
>
>  For instance:  output of running print_r on a very short dictionary from
>> PHP:
>>
>> Array
>> (
>>  [key3] => thing3
>>  [key2] => thing2
>>  [key1] => thing1
>> )
>>
>> And running pprint on the same dict in Python:
>>
>> {'key1': 'thing1', 'key2': 'thing2', 'key3': 'thing3'}
>>
>>
>> I finally decided that a good project would be building a quick function
>> that recreates the print_r function, and I got that working.  New
>> function output, in Python:
>>
>> (
>>  [key3] => thing3
>>  [key2] => thing2
>>  [key1] => thing1
>> )
>>
>
>
> You should probably use {} instead of () as the delimiter to indicate that
> the thing is a dictionary and not a tuple. but otherwise that's a good
> starter project.
>
>
Good idea.  I went with () because I was just trying to duplicate the
output in PHP.  Now that I've got that, I can start thinking about
improvements.  Varying the punctuation to match the type would probably be
a good idea.

Thanks,
  Andy
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello, and a newbie question

2013-04-16 Thread Steven D'Aprano

On 17/04/13 01:58, Andy McKenzie wrote:


1) Python 2.7 or 3.x?  I know I'm going to want to do some work with NLTK
(which appears to only have an alpha version out for Python 3), but I've
just gone through the hassle of dealing with an upgrade from PHP 4 to 5.3,
and I'd rather not start learning something that's already obsolete.  Any
words of advice?


Python 3.3 is awesome and much cleaner and better than 2.7, and 2.7 is
pretty damn good! So if you have a choice, pick 3.3. It's the future of
Python, 2.7 is the past.

But, 2.7 is still good, and if you need NLTK *right now* you might not have
a choice. (Unless you like being a guinea pig working with an alpha version.)

Also, the *incompatibilities* between 2.7 and 3.3 are fairly small. The
biggest difference from a beginner's perspective is that print is no longer
a statement, it is a function, so instead of writing this:

print "Hello world!"

you have to write this:

print("Hello world!")

That doesn't seem too onerous, does it? If you can cope with a few differences
of that complexity, why not learn both?



2) Best practices.  I have the WROX Press Beginning Python book, which
targets Python 2.  Clearly that's of only limited value if I'm going to go
with Python 3, but it looks like it's at least going to be a good overview.
  But some of the stuff they do seems to be fairly personalized, rather than
trying to follow standards.  Should I just start out with the tutorial from
docs.python.org?  I would assume that that would start putting me in the
right habits from the beginning... is that accurate, or is there a better
way to go?


If there's a Python 3 version of "Learning Python", from O'Reilly Books (sorry
I forget the authors), give it a go. The first edition, at least, is an awesome
book although you will want a more recent version since the first edition
deals with Python 1.5, which truly is ancient history!

I haven't done the official Python tutorial, but from what I've seen of it,
it's pretty good.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello, and a newbie question

2013-04-16 Thread eryksun
On Tue, Apr 16, 2013 at 7:57 PM, Virgilio Rodriguez Jr
 wrote:
> Can someone please do me the favor and remove me from this god forsaken
> email list I am sorry I signed up all it has done is taken over my phone and
> rings all night long with emails I am not interested in any more because it
> is just too many darn emails. I keep trying to log in and nothing it will
> not let me unsubscribe and it is BS already.

You can get a password reminder here:
http://mail.python.org/mailman/options/tutor

If you're sure that you have the correct password and still can't
unsubscribe, then email the administrator, tutor-ow...@python.org.
Alan Gauld is active in this thread, BTW, in case you happen to have
read the admin page where it says "Tutor list run by wescpy at
gmail.com, alan.gauld at btinternet.com".
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello, and a newbie question

2013-04-16 Thread eryksun
On Tue, Apr 16, 2013 at 8:15 PM, eryksun  wrote:
>> Can someone please do me the favor and remove me from this god forsaken
>> email list I am sorry I signed up all it has done is taken over my phone and
>> rings all night long with emails I am not interested in any more because it
>> is just too many darn emails. I keep trying to log in and nothing it will
>> not let me unsubscribe and it is BS already.
>
> You can get a password reminder here:
> http://mail.python.org/mailman/options/tutor
>
> If you're sure that you have the correct password and still can't
> unsubscribe, then email the administrator, tutor-ow...@python.org.
> Alan Gauld is active in this thread, BTW, in case you happen to have
> read the admin page where it says "Tutor list run by wescpy at
> gmail.com, alan.gauld at btinternet.com".

I just went through the steps. You don't even need your password.
Enter your email address in the bottom field of the list info page:

http://mail.python.org/mailman/listinfo/tutor

Click the button that says "Unsubscribe or edit options". Then simply
click the "Unsubscribe" button in the middle of the options page.
You'll get a confirmation email with instructions to complete the
process.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello, and a newbie question

2013-04-16 Thread Sander Sweers
On 04/17/2013 02:34 AM, eryksun wrote:
> I just went through the steps. You don't even need your password.
> Enter your email address in the bottom field of the list info page:
> 
> http://mail.python.org/mailman/listinfo/tutor
> 
> Click the button that says "Unsubscribe or edit options". Then simply
> click the "Unsubscribe" button in the middle of the options page.
> You'll get a confirmation email with instructions to complete the
> process.

Or send an e-mail to tutor-requ...@python.org with unsubscribe in the
subject.

~sander
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] regex to promote Py 2 to Py 3?

2013-04-16 Thread Jim Mooney
I already tried an example I copied from something online, in Py 2,
that had a ton of print statements. So I did some fast search and
replace to make them Py 3 with (), since I'm using Py 3. (Except Wing
101 won't do Replace All for some reason I haven't figured out, so
it's a bit tedious)

So for online progs, examples, ebooks, etc, that still have Py 2
examples I can copy, has anyone come up with a nice regex package to
do most of the conversions and produce a mostly Py3 from a Py2? That
might make some things a bit easier. I use Clipmate, which has a regex
option so I could just copy a Py2 script, run the regex, and paste
back a Py3. I could probably work one out but since I don't know Py
yet, that could be problematic ;')

Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] regex to promote Py 2 to Py 3?

2013-04-16 Thread Michael Weylandt


On Apr 16, 2013, at 21:02, Jim Mooney  wrote:

> I already tried an example I copied from something online, in Py 2,
> that had a ton of print statements. So I did some fast search and
> replace to make them Py 3 with (), since I'm using Py 3. (Except Wing
> 101 won't do Replace All for some reason I haven't figured out, so
> it's a bit tedious)
> 
> So for online progs, examples, ebooks, etc, that still have Py 2
> examples I can copy, has anyone come up with a nice regex package to
> do most of the conversions and produce a mostly Py3 from a Py2? That
> might make some things a bit easier. I use Clipmate, which has a regex
> option so I could just copy a Py2 script, run the regex, and paste
> back a Py3. I could probably work one out but since I don't know Py
> yet, that could be problematic ;')

Much ink has been spilled on just such a translation, but you may as well start 
with the 2to3 script and see if that suffices for your needs:

http://docs.python.org/2/library/2to3.html


Michael

> 
> Jim
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] regex to promote Py 2 to Py 3?

2013-04-16 Thread eryksun
On Tue, Apr 16, 2013 at 9:02 PM, Jim Mooney  wrote:
> So for online progs, examples, ebooks, etc, that still have Py 2
> examples I can copy, has anyone come up with a nice regex package to
> do most of the conversions and produce a mostly Py3 from a Py2? That
> might make some things a bit easier.

Generally the 2to3 script does an OK job. If you're using Windows it's
[Python_Dir]\Tools\Scripts\2to3.py.

http://docs.python.org/3/library/2to3

lib2to3 doesn't have a stable API, but you can use it in the REPL if
you want to experiment:

>>> from lib2to3.refactor import RefactoringTool
>>> rt = RefactoringTool(['lib2to3.fixes.fix_print'])

>>> code2 = r'''
... for s in strings:
... print >>myfile, s,
... '''

>>> code3 = str(rt.refactor_string(code2, ''))
>>> print(code3)

for s in strings:
print(s, end=' ', file=myfile)

If you want all the fixes applied, you can use

>>> from lib2to3.refactor import (
...   RefactoringTool, get_fixers_from_package)

>>> rt = RefactoringTool(get_fixers_from_package('lib2to3.fixes'))

>>> code2 = r'''
... class Test(object):
... __metaclass__ = MetaTest
... '''
>>> code3 = str(r.refactor_string(code2, ''))
>>> print(code3)

class Test(object, metaclass=MetaTest):
pass
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How did this decimal error pop up?

2013-04-16 Thread Jay Lozier

On 04/16/2013 01:43 PM, Jim Mooney wrote:
I was doing a simple training prog to figure change, and wanted to 
avoid computer inaccuracy by using only two-decimal input and not 
using division or mod where it would cause error. Yet, on a simple 
subtraction I got a decimal error instead of a two decimal result, as 
per below. What gives?


cost = float(input('How much did the item cost?: '))
paid = float(input('How much did the customer give you?: '))
change = paid - cost

*#using 22.89 as cost and 248.76 as paid*

twenties = int(change / 20)
if twenties != 0:
  twentiesAmount = 20 * twenties
  change = change - twentiesAmount
*#change is 5.8745, not 5.87 - how did I get this decimal 
error when simply subtracting an integer from what should be a

  #two-decimal amount?
*  print(twenties, ' twenties')
  print(change)

#and so forth for the rest of the change

--
*Jim Mooney
* 
This is a common problem with decimal numbers that is discussed in 
various numerical methods texts and is independent of the programming 
language. The two numerical texts I have from the 1980's discuss this 
problem using FORTRAN and Pascal ( I am fairly certain they are out of 
print). It caused by binary representation of decimal numbers at times 
will introduce errors in decimal numbers. Many decimal numbers can not 
be exactly represented by a binary number with some binary 
representations being a little higher and some a little lower than the 
actual number. There are recommend algorithms and data encoding 
techniques to use to minimize these problems.


Integers always have an exact binary representation so if it one can 
change the problem to one that uses integers instead of floats then the 
rounding problem disappears. Many financial calculations can be 
converted from dollars to cents to avoid decimals. Unfortunately, many 
science and engineering calculations will need to address the decimal to 
binary to decimal representation errors.


It can be a nasty problem if one does not address it properly an even 
larger error could occur. Often it is suitable error because one does 
not spot that an intermediate calculation is incorrectly implemented.


--
Jay Lozier
jsloz...@gmail.com

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] regex to promote Py 2 to Py 3?

2013-04-16 Thread Jim Mooney
> Generally the 2to3 script does an OK job. If you're using Windows it's
> [Python_Dir]\Tools\Scripts\2to3.py.
>
> http://docs.python.org/3/library/2to3


Thanks. I didn't know where to find it and thought I had to install it.

I opened a command window in my Python33/progs dir, started Python,
and ran it on a tiny test program called loops.py but I get "invalid
syntax" Any idea why?:

>>> 2to3 loops.py
  File "", line 1
2to3 loops.py
   ^
SyntaxError: invalid syntax

The loops.py program simply had the old-style print as a single test
statement. ie:

print 'Machiavelli'

Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor