Scientific Notation in Python and NumPy


Python can deal with floating point numbers in both scientific and standard notation. This post will explains how it works in Python and NumPy. If you just want to suppress scientific notation in NumPy, jump to this section.

You can create numbers with scientific notation in Python with e:


# Expected result
# 0.000345

Notice: the number is printed in standard notation even though we defined it with scientific notation. This is because Python decides whether to display numbers in scientific notation based on what number it is. As of Python 3, for numbers less than 1e-4 or greater than 1e16, Python will use scientific notation. Otherwise, it uses standard notation.

But you can override this behavior with string formatting. Use <variable>:<width>.<precision>e in your string formatting to display a number in scientific notation:

x = 3.45e-4

# Expected result
# 3.45e-04

To suppress scientific notation, use <variable>:<width>.<precision>f:


# Expected result
# 0.000345

With slight modifications, you can also use the format() or % string formatting approaches:


# Expected result
# 3.4500e-04

print("%.7f" % x)

# Expected result
# 0.0003450


Finally, in NumPy, you can suppress scientific notation with the np.set_printoptions() function:

import numpy as np

np.arange(5) / 100000

# Expected result
array([0.     , 0.00001, 0.00002, 0.00003, 0.00004])

You can read more in the docs if you want to change other characteristics of NumPy’s array printing.

Leave a Comment

Your email address will not be published. Required fields are marked *