Yet another command line calculator

Feb 02, '05 08:43:00AM

Contributed by: Anonymous

Python is included in OS X. It has an interactive mode that works very well as a calculator, as seen here. In the Terminal, launch Python as shown, and then type in the commands next to the >>> and ... prompts.

$ python
Python 2.3 (#1, Sep 13 2003, 00:49:11) 
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 45*3
135
>>> quit
'Use Ctrl-D (i.e. EOF) to exit.'
$
Since it is a complete programming language, you can write functions to calculate virtually anything. The following example is from the More Flow Control Tools section of the Python manual:
>>> def fib(n):    # write Fibonacci series up to n
...     """Print a Fibonacci series up to n."""
...     a, b = 0, 1
...     while b < n:
...         print b,
...         a, b = b, a+b
... 
>>> # Now call the function we just defined:
... fib(2000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
[robg adds: Note that in the example above, those are tabs inserted after the ..., and they're very important. Without them, you'll get an error (can you tell I haven't used Python much??)]

Comments (20)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050131140857217