How to do math in Python

1 min

Python provides various built-in mathematical operations and functions that can be used to perform mathematical calculations.

Here are some examples of basic mathematical operations in Python:

#addition
result = 3 + 4

#subtraction
result = 5 - 2

#multiplication
result = 3 * 4

#division
result = 8 / 2

#exponentiation
result = 2 ** 3
\

You can also use the math module to perform more advanced mathematical operations such as trigonometric functions, logarithms and more.

import math

#square root
result = math.sqrt(16)

#sine
result = math.sin(math.pi/2)

#logarithm
result = math.log10(100)

You can also use the NumPy library which provides advanced mathematical functions such as linear algebra, Fourier transform, and more.

In addition to that, python also has some libraries specific for scientific computations like scipy and pandas which will allow you to perform a wide range of mathematical and scientific computations.