DataScience Workbook / 05. Introduction to Programming / 3. Introduction to Python Programming / 3.4 Math Module - Various Mathematical Functions
Before starting this tutorial, there are a few requirements that you should have in place.
- Firstly, you should be familiar with using the command line in terminal on your computer.
Learn more from the practical turtorials in section 02. Introduction to the Command Line ⤴
- Secondly, you should have Python installed on your local machine.
Learn more from the practical turtorial Local Python setup on your computing machine ⤴
- Thirdly, you should have a Python Integrated Development Environment (IDE) set up on your computer, e.g., PyCharm or JupyterLab or any text editor.
Learn more from the practical turtorial Python Programming Environment(s) ⤴
- Finally, you should have a basic understanding of Python programming.
Learn more from the practical turtorial Introduction to Python Programming ⤴
Math in Python
Mathematics is a fundamental aspect of programming, and Python provides a range of tools and resources for working with math. Python provides a robust set of tools for working with math, including:
- built-in operators, for example:
- built-in functions, for example:
- built-in modules like
- the ability to import external libraries for more specialized tasks, for example:
math module is a built-in module in Python that provides access to many mathematical functions. It includes functions that provide more advanced mathematical operations such as:
In addition to built-in modules, Python also provides a way to import external libraries, such as
Pandas. These libraries contain a range of specialized functions and tools for working with specific areas of mathematics and data analysis.
When use math module?
math module in Python can help you perform complex mathematical operations with ease. This module is commonly used in scientific and mathematical computations and can be a useful tool for data analysts, engineers, and scientists who work with numerical data. In this module, you can perform various mathematical operations, such as finding the square root of a number, finding the value of a mathematical constant such as pi, and much more.
You can try out the
math module on your local machine if you have Python installed.
If you want to make up for the installation step, you can follow the installation guide provided in the tutorial Local Python setup on your computing machine ⤴.
Alternatively, if you prefer to work online, you can use various online Python interpreters such as Repl.it ⤴, PythonAnywhere ⤴, or Colab ⤴. These online interpreters provide a Python shell and allow you to run Python code without installing Python on your local machine.
For example, you can use online Python shell immediately available at https://www.python.org/shell/ ⤴ to experiment with the
Simply type in your Python code in the shell and hit enter to see the output.
Python has several built-in operators for basic arithmetic operations, such as:
In addition to these, Python also provides operators for more advanced mathematical operations, such as:
%for finding the remainder of a division
- integer division:
//for performing integer division
**for raising a number to a power
x = 10 y = 3 # Addition z = x + y print(z) # Output: 13 # Subtraction z = x - y print(z) # Output: 7 # Multiplication z = x * y print(z) # Output: 30 # Division z = x / y print(z) # Output: 3.3333333333333335
Python has an exponentiation operator
** that raises a number to a power.
x = 2 y = 3 # Exponentiation z = x ** y print(z) # Output: 8
• Integer Division
In Python, integer division is performed using the double slash
x = 23 y = 5 # Integer division z = x // y print(z) # Output: 4
In Python, the modulus operation is performed using the percent
x = 23 y = 5 # Modulus operation z = x % y print(z) # Output: 3
Python also has many built-in functions for performing mathematical operations.
|returns the absolute value of a number|
|round(x, y)||round(3.14159, 2)
|rounds a number (x) to a specified number (y) of decimal places|
|return the sum of the values in a list|
|return the minimum value in a list|
|return the maximum value in a list|
The math module provides a range of additional mathematical functions, including trigonometric functions, logarithmic functions, statistical functions, and more.
MATH is a built-in module (no installation required) that contains various mathematical constants and more advanced mathematical operations such as trigonometric functions, logarithmic functions, and statistical functions, among others.
The math module does not contain functions for basic arithmetic operations such as addition, subtraction, multiplication, and division. These are basic mathematical operations that can be performed using the standard arithmetic operators in Python, such as +, -, *, /.
In the tabs below you can find the corresponding tables with
math module functions along with function description and usage examples.
|Returns the square root of x.|
|Returns the factorial of x.|
|Rounds up a number to the nearest integer.|
|Rounds down a number to the nearest integer.|
|Returns the integer value of x (truncates towards zero).|
|Returns the absolute value of provided number in the same type.|
|Returns the absolute value of provided number as a float-point number.|
|Returns the natural logarithm (base e) of x.|
|Returns the base-10 logarithm of x.|
|Returns the exponential value of x.|
|math.pow(x, y)||math.pow(2, 3)|
|Returns the value of x raised to the power of y.|
|math.copysign(x, y)||math.copysign(10, -1)|
|Returns a value with the magnitude of x and the sign of y.|
|math.fmod(x, y)||math.fmod(10, 3)|
|Returns the remainder of x/y as a float.|
|math.gcd(x, y)||math.gcd(24, 36)|
|Returns the greatest common divisor of x and y.|
|math.hypot(x, y)||math.hypot(3, 4)|
|Returns the Euclidean norm, sqrt(x2 + y2).|
|math.e||math.e||A mathematical constant that represents the value of e.|
|math.inf||math.inf||Represents positive infinity, which is a value greater than any finite number.|
|math.nan||math.nan||Represents a value that is not a number (NaN).|
|math.pi||math.pi||A mathematical constant that represents the value of pi.|
|math.tau||math.tau||Represents the mathematical constant tau, which is equal to 2*pi.|
|math.sin(x)||math.sin(math.pi/2)||Returns the sine of an angle in radians.|
|math.cos(x)||math.cos(math.pi)||Returns the cosine of an angle in radians.|
|math.tan(x)||math.tan(math.pi/4)||Returns the tangent of an angle in radians.|
|Converts an angle from degrees to radians.|
|Converts an angle from radians to degrees.|
|Returns the arc sine (in radians) of x.|
|Returns the arc cosine (in radians) of x.|
|Returns the arc tangent (in radians) of x.|
|math.atan2(y, x)||math.atan2(1, 1)|
|Returns the arc tangent (in radians) of y/x, in the range [-pi, pi].|
|Returns True if x is NaN (not a number), False otherwise.|
|Returns True if x is positive or negative infinity, and False otherwise.|
|Returns True if x is a finite number (i.e., not infinity or NaN), and False otherwise.|
|math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)||math.isclose(3.5, 3.6, rel_tol=0.1)|
|Returns True if a is close in value to b, False otherwise.|
Import MATH module
math module is built into Python and does NOT require any installation.
To use the
math module, you need to import it first using the
Once the math module is imported, you can access all its functions by prefixing them with math, followed by a dot and the function name,
For example, to find the square root of a number, you can use the
- 3.5 Pandas Library - Data Structure Manipulation Tool
- 3.6 Numpy Library - Multi-Dimensional Arrays Parser
- 4. Introduction to R programming