Python Scientific Notation

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

Python scientific notation is a way of writing a large or a small number in powers of 10. Any number can be written in its scientific notification form by multiplying the number between 1 and 10 by a power of 10 (a10ba*10^b). Scientific notation reduces the format of the number representation.

Types of Numbers in Python

Four different types of numbers are used in Python :

  • Integer(int)
  • Floating Points/Real numbers (float)
  • Boolean (bool)
  • Complex number (j)

Floating point numbers can be represented in Python scientific notation. A very small or a very large number can be written in Python scientific notation. Let's take an example of the representation of floating point numbers in Python scientific notation.

floating point number = 0.000009732
The floating point number above can also be written as 9.7321069.732*10^{-6}.
The Python scientific notation of 9.7321069.732 * 10^{-6} can be written as 9.732e-6. Here e is the Exponent symbol. We can use e or E as it is not case-sensitive.

Representing Float in Scientific Notation by "repl"

REPL stands for Read Evaluate Print Loop and is the name given to the interactive MicroPython prompt that is accessible on Pycom devices. Using the REPL is by far the easiest way to test out Python code and run commands. You can use the REPL in addition to writing scripts in the main.py file.

Floating point numbers are always stored in numeric values and not in the form of scientific notation. While we print the floating point number it is printed in Python scientific notation depending on the condition.

If the floating point number is greater than e4e{-4} and less than e15e{15} it is represented in decimal form by REPL. Otherwise, REPL prints the floating point number in Python scientific notation form.

Let's take some examples and try to print the type of floating point number and the number itself.

Output :

For both the floating point number represented by x and y, the type of both the numbers is float as both the numbers are stored as numeric values. As the number y is less than e4e{-4} so it is printed in Python scientific notation form.

Syntax of Python Scientific Notation

The syntax for Python scientific notation is :

Where N can be any number greater than 0 and less than 10 and M can be any integer.

Examples :

  • 1.23e3
  • 4.78e-4

Parameters of Python Scientific Notation

For a number represented in Python scientific notation form i.e NeMNe^M, N should be any number greater than 0 and less than 10 and M should be any integer.

Printing a Number in Scientific Notation in Python

To print a number in scientific notation in Python, str.format() method is used. Next, we will see some examples of how the str.format() method is used to print the number in scientific notation in Python.

Examples

1. Format Scientific Notation in Python

If we have to format 0.000009732 in Python scientific notation then the code for it will be

Here the {:e} in the string is the key point to represent a float value in Scientific Notation while printing the number.

Output :


2. Print Scientific Notation in Python

If we have to print 17320000 in Python scientific notation then the code for it will be

Output :


3. Removing Zeros from Decimal

To remove zeros after the decimal the following code is written for it.

Output :

Suppressing Scientific Notation in Python

Suppressing scientific notation means representing the values in a full decimal format without defining them in Python scientific notation form. Python automatically represents numbers less than e04e{-04} in scientific notation format but suppressing these numbers will represent the number in full decimal format.

For example, if the scientific notation form of the number is 1.229e+05, then it will be suppressed to full decimal format and will be represented as 122900.

Examples of Suppressing Scientific Notation in Python

To represent a number 'num' in the decimal format up to n decimal places we use the syntax f”{num:.nf}”

Example - 1 :

Output :

Example - 2 :

Output :

We can suppress scientific notation using a different syntax also which is str.format(num) with str as “{:.nf}”

Example - 3 :

Output :

Python Scientific Notation in Regex

Regex is used in programming to extract any pattern from a string. If we need to extract the scientific notation from a text then we have to write the regex pattern for identifying the scientific notation format.

Regex for scientific notation format for patterns like nemne^{m}, where n is the float value or integer and m is the integer to the power of e. Let's write the regex for identifying scientific notation format from the text.

You can read more about Regex in Python from here

Output :

The regex pattern used above is explained below :

  • [-+] – Token Matches for either or +.
  • ? – Matches the previous token between zero and one time.
  • [\d] – Token Matches to a digit
  • . – Matches for . literally.
  • [Ee] – Token matches for either E or e.

Python Matplotlib to Convert to Scientific Notation

In matplotlib graphs, the axis offset values are represented in Python scientific notation format by default. To convert these offset values to plain decimal values we need to change the tick label format from style to plain. This changes the default scientific notation values to plain decimal values.

Let's draw a matplot lib graph by converting the offset values to decimal values from scientific notation format.

Output : output-conversion-of-scientific-notation-using-matplotlib

Scientific Notation in Jupyter Notebook

Jupyter notebook is a web-based interactive environment for writing Python code in Notebook document format.

We can write the Python scientific notation and all its conversion functions similar to what we write in Python shell. In Jupiter's notebook, the code will be organized in a notebook document format.

Jupyter notebook can be converted to any standard format such as HTML, PDF, LAtex, or Python.

Sample code is written in the Jupyter notebook of google collab :

example-of-sample-code-in-jupyter-notebook-of-google

Scientific Notation in Python Pandas

Pandas is a Python library used mainly for analyzing and manipulating data. It is used for working with values stored in tables like matrices.

Let's create a data frame in Python using pandas and NumPy in scientific notation format.

Output :

The above code creates a column containing five random numbers in scientific notation format.

The code creates five random numbers as we are using np.random to generate random numbers. To remove the scientific notation from the numbers generated we can use any of the following syntaxes.

  • df.round(n)
  • df.apply(lambda x: ‘%.nf’ % x, axis=1)
  • pd.set_option(‘display.float_format’, lambda x: ‘%.nf’ % x)
  • pd.options.display.float_format= ‘{:.nf}’.format

n in the above syntax denotes the number of elements.

Converting Integer to Scientific Notation in Python

We can convert the Integer to Python scientific notation using {.e} flag with the help of the format() method of string.{.e} converts any float or integer number to scientific notation.

Also, we can use {:.Ne} to limit the number of decimal digits in the scientific notation format.

Example :

Output :

Explanation :

In the first print statement no {:.Ne} is used to limit the number of decimal places in converting the integer.

In the second print statement, the {:.3e} limits only three-digit after decimal when the integer is converted to scientific notation format.

Removing Scientific Notation from Graph

To remove the scientific notation from the offset value of the axis in the graph the following syntax is used.

Example of matplot lib plotting :

matplot-lib-plotting-example

Conclusion

  • Python scientific notation is a way of writing a large or a small number in powers of 10.
  • If the floating point number is greater than e-4 and less than e15 it is represented in decimal form by REPL.
  • Python scientific notation simplifies the reading and understanding of very large or tiny numbers.
  • To print a number in scientific notation in Python, str.format() method is used.
  • f”{num:.nf}” syntax is used for suppressing scientific notation in Python.
  • {.e} and format method is used to convert any integer or float value to scientific notation format.