[Edu-sig] using Python as a calculator

David MacQuigg macquigg at ece.arizona.edu
Sat Apr 10 22:41:11 CEST 2010


Christian Mascher wrote:
> Edward Cherlin wrote:
>> [sigh]
>>
>> Do math tables in a math array language.
>>
>> degrees =. i. 91  NB. 0..90
>>
>> radians =. degrees * o. % 180
>>
>> table =. |: degrees, 1 2 3 o./ radians
>
> Sorry, I don't know J (Kirby does), but this is exactly the reason I 
> prefer Python. Readability counts (for me). For creating a table, most 
> people would probably use a spreadsheet anyway, but as I happen to 
> know Python, I use it for such tasks from time to time. I can even 
> remember the syntax without having used Python for months. Don't think 
> that would be the case with J. Not very inclined to learn that.

I agree.  Clarity is important, especially with young students (and us 
old guys who have trouble remembering Java classes :>).

from math import sin, cos, tan, pi

rad_per_degree = pi/180
pattern = "{0:>5g}    {1:.9f}    {2:.9f}    {3:e}"

def print_trig_table(start, stop, step):

.    for degrees in range(start, stop, step):
       
.        theta = degrees * rad_per_degree
.        data = ( degrees, cos(theta), sin(theta), tan(theta) )
.        print( pattern.format(*data) )


The only thing a little non-intuitive about this is the construct 
pattern.format(*data).  It's the same problem as with "".join(list).  It 
feels backwards until you really grasp the concept of these methods 
being associated with string objects, not with the data, which can be 
any of various object types.

Back to the subject of Python as a calculator, I have offered to mentor 
a proposal in Google Summer of Code seeking to improve IDLE.  A key item 
is adding the ability to display graphics.  This should be as easy as 
just pressing the GRAPH button, but also not limit students who want the 
full sophistication of a package like matplotlib.  Suggestions are welcome.





More information about the Edu-sig mailing list