Am Thu, 20 Sep 2012 05:05:11 -0400
schrieb Philip Webb purs...@ca.inter.net:
120919 Marc Joliet wrote:
120918 Philip Webb purs...@ca.inter.net wrote:
With Python running as interpreter, I would get much more capability,
but I would need to enter the special line to load the math functions :
is it possible to do it with some capitalised variable in .bashrc ,
which might list parameters telling Python3 what to load when it starts ?
one of the 'man' files seems to refer to something like that, but briefly.
3.) Put the import line in its own file and put it in the variable
PYTHONSTARTUP, e.g. export PYTHONSTARTUP=/path/to/my/script.py.
Python executes it's contents before presenting the prompt,
so you can put whatever imports you want in that script.
Thanks, that's what I saw in my brief glance at the 'man'.
It works out of the box: the only problem is precision,
which at 16 decimal places is a bit more than I usually need (smile).
I can search out how to limit it to something more useful to me,
but might you have a quick answer ? Thanks for the above.
Reading up the format specification mini language
(http://docs.python.org/library/string.html#formatspec, and the format syntax
explained above it), you could do as follows, to print as float rounded to four
decimal places:
print('{0:.4f}'.format(2.4))
Or, leaving out the zero (you only need the indexes if you print things out of
order or multiple times):
print('{:.4f}'.format(2.4))
Also, I re-remembered that there is an alternative formatting method (I don't
print formatted output that often in python, I guess):
print(%.4f % 2.4)
will do the same as the above two examples. Either way, to make things easy,
you could define your own print function to do that for you, e.g.:
def myprint(num, places=4, *args, **kargs):
fmt_str = {:. + str(places) + f}
print(fmt_str.format(num), *args, **kargs)
Using it would look like (in IPython):
In [13]: myprint(2.4)
2.4000
In [14]: myprint(2.4, 5)
2.4
You would put this in the startup script after the import line. Note that it
passes extra positional and keyword arguments to print(), so you can specify a
file to print to, for example. Also note that because of this, it won't work in
Python 2.
HTH
--
Marc Joliet
--
People who think they know everything really annoy those of us who know we
don't - Bjarne Stroustrup
signature.asc
Description: PGP signature