ipython - How do I control number formatting in the python interpreter? -


I often use Python interpreter to perform quick numerical calculations and would like to automatically print all numerical results, For example, exponential notation. Is this a way to set up for the entire session?

For example, I want to:

  gt; & Gt; 1.e12 1.0e + 12  

No:

  & gt; & Gt; & Gt; 1.e12 1000000000000.0  

Whatever you want, create a Python script ( Mystartup.py ) and its After setting an environment variable PYTHONSTARTUP in the path of this script, then Python will load this script when an interactive session starts (but not run the script). In this script, define a function like this:

  def _ (v): If type (V) == type (0.0): print "% e"% v else: print v < / Code> 

Then, in an interactive session:

 C: \ temp> Set PYTHONSTARTUP = mystartup.py C: \ temp> Python ActivePython 2.5.2.2 (ActiveState Software Inc .) Based on the Python 2.5.2 (r252: 60 9 11, 27 March 2008, 17:57:18) [MSC v.1310 32 bit (Intel)] Win32 Type "Help", "Copyright", "Credit" Or "license for more information" >>> _ (1e12) 1.000000e + 012 >>> _ (14) 14 >>> _ (14.0) 1.400000e + 001 >>> 

Of course, You define the function Committee called Can you whaetver and really want to work.

Even better, it will be used to use it. It's great, and you can set number formatting by using result_display.when_type (some_type) (my_print_func) (see IPython site or search for more information about how to use it Please).


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -