[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

MATHEMATICal Calculations IN PYTHON WITH MATH

[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1570689383587{background-color: #f6f6f6 !important;}”]

In Python there is the standard math library that makes it easy to perform numeric operations. For example, the logarithm, square root, and trigonometric functions are built-in and can be used directly in the standard library.

[/vc_column_text][vc_empty_space height=”22px”][vc_column_text]

HOW TO USE THE MATH MODULE IN PYTHON?

Python has a built-in standard module, math, which contains many methods for performing ordinary calculations. This module greatly facilitates our work when we are writing operations to perform some mathematical calculations. We will show some examples below how we use it in some operations. For a complete list of all operations available, see math – mathematical functions (Python docs).

 

In chapter 5 we will see more about how functions in Python and how we can create our own functions, but for now, we are want to see how we can use the built-in function Math to more easily perform different mathematical calculations. Keep in mind that it is important to keep track of what data type (input) that is compatible with the calculation you intend to use

 

This module greatly facilitates out work when we are performing mathematical calculations

[/vc_column_text][vc_empty_space height=”22px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

EXAMPLE OF FUNCTIONS IN THE MATH MODULE

Below are some examples of functions found in the math module and examples of how to use them.

 

Before we start using the Math module, we need to import the library by writing.

 

import math

Note, this only needs to be done once.[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

Raised to the power of – MATH.POW (X, Y)

For example, say you would like to calculate 2 ^ 8 (two raised by eight). It may be difficult (and tiring) to count on paper, but in Python you make it easy with the pow (x, y) function.

 

power = math.pow(2,8)
print(power)

Giving the result:

 

256.0

[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

Square root of – MATH.SQRT (X, Y)

The square root is easily calculated with the sqrt (x) function

 

sqrtroot = math.sqrt(12)
print(sqrtroot)

Giving the result:

 

3.4641016151377544

[/vc_column_text][vc_empty_space height=”10px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

Exponential function- MATH.EXP (X)

The exponential function e ^ x is easily calculated with the exp (x) function

 

exp = math.exp(2)
print(exp)

Giving the result

 

7.38905609893065

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_empty_space height=”20px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row]