n>
x = <span class="hljs-number">-4.0</span>
fabs_x = <span class="hljs-built_in">math</span>.fabs(x)
<span class="hljs-built_in">print</span>(<span class="hljs-string">'fabs(x) is'</span>, fabs_x)</pre></div><p id="5c74">Output:</p><div id="c5d0"><pre><span class="hljs-function"><span class="hljs-title">fabs</span><span class="hljs-params">(x)</span></span> is <span class="hljs-number">4.0</span></pre></div><p id="52f3">Another function is<code>sqrt()</code>. It takes one parameter that should be an integer or a float number and the square root of this number as a float number, this number.</p><div id="2036"><pre>import <span class="hljs-built_in">math</span>
x = <span class="hljs-number">4</span>
sqrt_x = <span class="hljs-built_in">math</span>.<span class="hljs-built_in">sqrt</span>(x)
<span class="hljs-built_in">print</span>(<span class="hljs-string">'sqrt(x) is'</span>, sqrt_x)</pre></div><p id="4031">Output:</p><div id="24fe"><pre><span class="hljs-function"><span class="hljs-title">sqrt</span><span class="hljs-params">(x)</span></span> is <span class="hljs-number">2.0</span></pre></div><p id="7846">Another function is <code>pow()</code>. It takes two arguments, the first one is <code>x</code> which is a number (an integer or float). The second argument is <code>y</code> which is the power and should be an integer. This function returns the value of <code>x</code> to the power of <code>y</code> as a float number.</p><div id="b55c"><pre>import <span class="hljs-built_in">math</span>
pow_x = <span class="hljs-built_in">math</span>.<span class="hljs-built_in">pow</span>(x, <span class="hljs-number">3</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">'pow(x,3) is'</span>, pow_x)</pre></div><p id="02cd">Output:</p><div id="a8dc"><pre><span class="hljs-attribute">pow</span>(x,<span class="hljs-number">3</span>) is <span class="hljs-number">64</span>.<span class="hljs-number">0</span></pre></div><p id="c273">Another function is<code>log()</code>. This is the logarithm function. It takes two parameters. The first parameter should be an integer or a float number. The second parameter is optional, and it represents the <code>base</code> of the logarithm (natural logarithm is calculated by default).</p><div id="760e"><pre>import <span class="hljs-built_in">math</span>
log_x = <span class="hljs-built_in">math</span>.<span class="hljs-built_in">log</span>(x)
<span class="hljs-built_in">print</span>(<span class="hljs-string">'log(x) '</span>, log_x)</pre></div><p id="41bd">Output:</p><div id="7f22"><pre><span class="hljs-function"><span class="hljs-title">log</span><span class="hljs-params">(x)</span></span> <span class="hljs-number">1.3862943611198906</span></pre></div><p id="56f3"><code>log()</code> is a very common function in mathematics, but most often, you are not going to use it if you are a programmer. However, if you are a data scientist or machine learning engineer, you have to understand exactly how logarithm function works.</p><h2 id="41c8">4. Rounding Functions</h2><p id="c057">In this section, two rounding functions,<code>ceil() and floor()</code> , will be covered.</p><p id="e6c6"><code>ceil()</code> takes one argument and it should be a float number and returns an integer number by rounding the passed number <b>upwards</b> to the nearest integer.</p><div id="c2f0"><pre>import <span class="hljs-built_in">math</span>
x = <span class="hljs-number">3.5893</span>
ceil_x = <span class="hljs-built_in">math</span>.<span class="hljs-built_in">ceil</span>(x)
<span class="hljs-built_in">print</span>(<span class="hljs-string">'ceil(x) is'</span>, ceil_x)</pre></div><p id="084a">Output:</p><div id="f46c"><pre><span class="hljs-function"><span class="hljs-title">ceil</span><span class="hljs-params">(x)</span></span> is <span class="hljs-number">4</span></pre></div><p id="3daf">You got 4 because 3.5893 has been rounded to the nearest integer upwards.</p><p id="46e5"><code>floor()</code> is the opposite of <code>ceil()</code>. It takes one argument (a float number) and returns an integer number by rounding the passed number <b>downwards</b> to the nearest integer.</p><div id="3d63"><pre>import <span class="hljs-built_in">math</span>
x = <span class="hljs-number">3.9</span>
floor_x = <span class="hljs-built_in">math</span>.<span class="hljs-built_in">floor</span>(x)
<span class="hljs-built_in">print</span>(<span class="hljs-string">'floor(x) is'</span>, floor_x)</pre></div><p id="0fd2">Output:</p><div id="b6d1"><pre><span class="hljs-function"><span class="hljs-title">floor</span><span class="hljs-params">(x)</span></span> is <span class="hljs-number">3</span></pre></div><h2 id="5065">5. Trigonometric Functions</h2><p id="6e57"><code>math</code> module is full of trigonometric functions. Some of them are shown below.</p><p id="ff2a"><code>sin()</code></p><div id="3c5e"><pre>import <span class="hljs-built_in">math</span>
x = <span class="hljs-number">3.14</span>
sin_x = <span class="hljs-built_in">math</span>.<span class="hljs-built_in">sin</span>(x)
<span class="hljs-built_in">print</span>(<span class="hljs-string">'sin(x) is '</span>, sin_x)</pre></div><p id="a49e">Output:</p><div id="47c2"><pre><span class="hljs-function"><span class="hljs-title">sin</span><span class="hljs-params">(x)</span></span> is <span class="hljs-number">0.0015926529164868282</span></pre></div><p id="113a"><code>math</code> also has all the other trigonometric functions like <code>cos()</code>, <code>tan()</code>, …etc, and you can use them exactly like the <code>sin()</code>.</p><p id="60c9">Another function is <code>degrees()</code>. It takes one argument which represents a value in radians and converts this ar
Options
gument into a value in degrees.</p><div id="c641"><pre><span class="hljs-keyword">import</span> <span class="hljs-built_in">math</span>
x = <span class="hljs-number">3.14</span>
degree_x = <span class="hljs-built_in">math</span>.degrees(x)
<span class="hljs-built_in">print</span>(<span class="hljs-string">'degrees(x) is'</span>, degree_x)</pre></div><p id="95a3">Output:</p><div id="fcc5"><pre><span class="hljs-function"><span class="hljs-title">degrees</span><span class="hljs-params">(x)</span></span> is <span class="hljs-number">179.9087476710785</span></pre></div><p id="9096">The opposite of the <code>degrees()</code><b> </b>function in Python is the <code>radians()</code><b> </b>function which converts a value from degrees to radians.</p><div id="5d38"><pre><span class="hljs-keyword">import</span> <span class="hljs-built_in">math</span>
x = <span class="hljs-number">180</span>
radians_x = <span class="hljs-built_in">math</span>.radians(x)
<span class="hljs-built_in">print</span>(<span class="hljs-string">'radians(x) is'</span>, radians_x)</pre></div><p id="16ec">Output:</p><div id="44ff"><pre><span class="hljs-function"><span class="hljs-title">radians</span><span class="hljs-params">(x)</span></span> is <span class="hljs-number">3.141592653589793</span></pre></div><h2 id="a9f7">5. Mathematical Constants</h2><p id="d488"><code>math</code> module also has some mathematical constants like <code>pi</code> and <code>e</code>. These mathematical constants are very common in mathematics.</p><div id="435c"><pre>import <span class="hljs-built_in">math</span>
<span class="hljs-built_in">pi</span> = <span class="hljs-built_in">math</span>.<span class="hljs-built_in">pi</span>
e = <span class="hljs-built_in">math</span>.e
<span class="hljs-built_in">print</span>(<span class="hljs-string">'Mathematical pi is'</span>, <span class="hljs-built_in">pi</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">'Euler Number is'</span>, e)</pre></div><p id="18d1">Output:</p><div id="2dea"><pre><span class="hljs-attribute">Mathematical</span> pi is <span class="hljs-number">3</span>.<span class="hljs-number">141592653589793</span>
<span class="hljs-attribute">Euler</span> Number is <span class="hljs-number">2</span>.<span class="hljs-number">718281828459045</span></pre></div><p id="c3c1">As you have seen, you don’t need parentheses because <code>pi</code> and <code>e</code> are constants, not functions.</p><p id="e92e"><b>Now, let us summarize what we have learned in this article.</b></p><figure id="643f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*EORD1Dzoyk968fA4bZmCig.jpeg"><figcaption>Photo by <a href="https://www.pexels.com/@ann-h-45017/">Ann H</a> on <a href="https://www.pexels.com/">pexels</a></figcaption></figure><ul><li><code>math</code> module is a built-in Python module and you do not need to install it if you want to use it.</li><li><code>math</code> module is full of useful mathematical functions including rounding functions, trigonometric functions, … etc</li></ul><p id="4462"><b><i>P.S.</i></b><i>: A million thanks for your time reading my story. Before you leave let me mention quickly two points:</i></p><ul><li><i>First, to get my posts in your inbox directly, would you please subscribe <a href="https://medium.com/@samersallam92/subscribe"><b>here</b></a></i>, <i>and you can follow me <a href="https://medium.com/@samersallam92"><b>here</b></a>.</i></li><li><i>Second, writers made thousands of <b>$$</b> on Medium. To get unlimited access to Medium stories and start earning,<a href="https://medium.com/@samersallam92/membership"><b> sign up now for Medium membership</b></a><b> </b>which<b> </b>only costs $5 per month. By signing up <a href="https://medium.com/@samersallam92/membership"><b>with this link</b></a>, you can directly support me at no extra cost to you.</i></li></ul><div id="6e28" class="link-block">
<a href="https://medium.com/@samersallam92/list/32d3a941c05e">
<div>
<div>
<h2>Python Complete Beginner to Expert Course</h2>
<div><h3>undefined</h3></div>
<div><p>undefined</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*847092937aa61cc34f9ce67fbf7a90ef448cf214.jpeg)"></div>
</div>
</div>
</a>
</div><p id="1e35">To get back to the previous article, you can use the following link:</p><p id="5c1a"><a href="https://blog.devgenius.io/operators-priorities-python-complete-course-part-17-1070b43d4127">Part 17:Operators Priorities</a></p><p id="724b">To move on to the next article, you can use the following link:</p><p id="620b"><a href="https://blog.devgenius.io/a-quick-overview-of-random-module-python-complete-course-part-19-24b5acba4bb2">Part 19:random Module</a></p><h2 id="7dd8">Resources:</h2><ul><li>GitHub <a href="https://github.com/samersallam/python-complete-beginner-to-expert-course/tree/main/math%20Module"><b>here</b></a><b>.</b></li></ul><div id="f4a9" class="link-block">
<a href="https://readmedium.com/mlearning-ai-submission-suggestions-b51e2b130bfb">
<div>
<div>
<h2>Mlearning.ai Submission Suggestions</h2>
<div><h3>How to become a writer on Mlearning.ai</h3></div>
<div><p>medium.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*ib0DX0UzRoFcNuZILb7rNA.jpeg)"></div>
</div>
</div>
</a>
</div></article></body>
A Quick Overview of math Module: Python Complete Course — Part 18
Learn why to use and how to use the math module in Python.
This article is a part of the Python Complete Beginner to Expert Course
which you can find it here.
All resources are available in the “Resources” section below.
This article is also available as a YouTube video here.
Introduction
Mathematical calculations are an essential part of most Python programs. Whether you are working on a scientific project, a financial application, or any other type of project, you can’t escape the need for math.
For straightforward mathematical calculations in Python, you can use the built-in mathematical operators, such as addition (+), subtraction (-), division (/), and multiplication (*).
However, more advanced operations, such as logarithmic, trigonometric, or power functions, are not supported as operators.
Does that mean you need to implement all of these functions from scratch? Fortunately, NO. Python provides a module specifically designed for mathematical operations which is the math module.
This is the first time this term “module” has been used in this course. Therefore, I will start by defining what a module is. It is a Python file with .py extension. It’s exactly like the files that you have already created and are using to write your code.
In Python, there are a lot of available built-in modules or third-party modules. In this case, you call the module a “library” because it has some ready codes for you to use directly.
You have to keep in mind that whenever you want to use codes from a module or a library, you MUST import this module to your workspace.
For that, you have the keyword import, and you can use it in this way:
import module_name
Now, let us start learning about math module.
2. math Module
It is a built-in module. In other words, it comes with the original Python package. Therefore, you don’t need to install it if you want to use it.
This module is full of mathematical functions that you will use a lot in your programs. For example, you have:
pow(): the power function.
sqrt(): the square root function.
fabs(): the absolute value function.
Now let us take some examples for better understanding.
3. General Functions
There are a lot of functions in this module. In this article, some of the common functions will be presented.
The first one isexp(). It takes a number as its input and returns a float number. This function returns the value of (e^x)where e is the Euler’s number and x is the passed number.
import math
x = 4
exp_x = math.exp(x)
print('exp(x) is ', exp_x)
Output:
exp(x) is 54.598150033144236
You can see that when you want to use a function from the math module, you have to use dot . between the module name and the function name. After that, you have to pass the arguments this function needs.
Another function isfabs() . It takes one argument that should be a number and returns the absolute value of this number.
importmath
x = -4.0
fabs_x = math.fabs(x)
print('fabs(x) is', fabs_x)
Output:
fabs(x) is 4.0
Another function issqrt(). It takes one parameter that should be an integer or a float number and the square root of this number as a float number, this number.
import math
x = 4
sqrt_x = math.sqrt(x)
print('sqrt(x) is', sqrt_x)
Output:
sqrt(x) is 2.0
Another function is pow(). It takes two arguments, the first one is x which is a number (an integer or float). The second argument is y which is the power and should be an integer. This function returns the value of x to the power of y as a float number.
import math
pow_x = math.pow(x, 3)
print('pow(x,3) is', pow_x)
Output:
pow(x,3) is 64.0
Another function islog(). This is the logarithm function. It takes two parameters. The first parameter should be an integer or a float number. The second parameter is optional, and it represents the base of the logarithm (natural logarithm is calculated by default).
import math
log_x = math.log(x)
print('log(x) ', log_x)
Output:
log(x)1.3862943611198906
log() is a very common function in mathematics, but most often, you are not going to use it if you are a programmer. However, if you are a data scientist or machine learning engineer, you have to understand exactly how logarithm function works.
4. Rounding Functions
In this section, two rounding functions,ceil() and floor() , will be covered.
ceil() takes one argument and it should be a float number and returns an integer number by rounding the passed number upwards to the nearest integer.
import math
x = 3.5893
ceil_x = math.ceil(x)
print('ceil(x) is', ceil_x)
Output:
ceil(x) is 4
You got 4 because 3.5893 has been rounded to the nearest integer upwards.
floor() is the opposite of ceil(). It takes one argument (a float number) and returns an integer number by rounding the passed number downwards to the nearest integer.
import math
x = 3.9
floor_x = math.floor(x)
print('floor(x) is', floor_x)
Output:
floor(x) is 3
5. Trigonometric Functions
math module is full of trigonometric functions. Some of them are shown below.
sin()
import math
x = 3.14
sin_x = math.sin(x)
print('sin(x) is ', sin_x)
Output:
sin(x) is 0.0015926529164868282
math also has all the other trigonometric functions like cos(), tan(), …etc, and you can use them exactly like the sin().
Another function is degrees(). It takes one argument which represents a value in radians and converts this argument into a value in degrees.
importmath
x = 3.14
degree_x = math.degrees(x)
print('degrees(x) is', degree_x)
Output:
degrees(x) is 179.9087476710785
The opposite of the degrees()function in Python is the radians()function which converts a value from degrees to radians.
importmath
x = 180
radians_x = math.radians(x)
print('radians(x) is', radians_x)
Output:
radians(x) is 3.141592653589793
5. Mathematical Constants
math module also has some mathematical constants like pi and e. These mathematical constants are very common in mathematics.
import mathpi = math.pi
e = math.e
print('Mathematical pi is', pi)
print('Euler Number is', e)
Output:
Mathematical pi is 3.141592653589793Euler Number is 2.718281828459045
As you have seen, you don’t need parentheses because pi and e are constants, not functions.
Now, let us summarize what we have learned in this article.
math module is a built-in Python module and you do not need to install it if you want to use it.
math module is full of useful mathematical functions including rounding functions, trigonometric functions, … etc
P.S.: A million thanks for your time reading my story. Before you leave let me mention quickly two points:
First, to get my posts in your inbox directly, would you please subscribe here, and you can follow me here.
Second, writers made thousands of $$ on Medium. To get unlimited access to Medium stories and start earning, sign up now for Medium membershipwhichonly costs $5 per month. By signing up with this link, you can directly support me at no extra cost to you.