In this lesson, we will get acquainted with the built-in module of the standard Python library. This module provides many functions for mathematical calculations. In order to speed up calculations, this module is “under the hood” written in C.
Table of Contents
The number representation functions
ceil() and floor() – integer part of a number
We have already considered these functions in a previous article. Let us briefly repeat them. ceil() and floor() are ways to perform rounding. Both take a number with a fractional part (float type) and return an integer (int type). The difference between them is that ceil() rounds the number up (to the nearest larger integer), while floor() rounds it down.
from math import floor, ceil
float_var = 3.14
first_int_var = floor(float_var)
second_int_var = ceil(float_var)
print(f'The number {float_var} has type {type(float_var)}')
print(f'Round down the number {float_var} and get {first_int_var}')
print('With type', type(first_int_var))
print(f'Round up {float_var} and get {second_int_var}')
print('With type', type(second_int_var))
# Output:
The number 3.14 has type
Round down 3.14 and get 3
With type
Rounding 3.14 up gets 4
With type
Don’t forget to import the math module into your project!
Function fabs() – number module
Like Python’s built-in abs function, the math.fabs function returns the modulus of a number (if the number is negative, the “-” sign is discarded). But there are important differences between them. First, math.fabs is not designed for working with complex numbers, and second, unlike abs, it returns a fractional number, not an integer.
from math import fabs
var = -3
first_int_var = abs(var)
second_int_var = fabs(var)
print(f'The number {var} has type {type(float_var)}')
print(f'Module of number {var} obtained by abs function: {first_int_var}')
print('With type', type(first_int_var))
print(f'Module of number {float_var}, obtained with function fabs: {second_int_var}')
print('with type', type(second_int_var))
print(abs(complex(1, 2)))
print(fabs(complex(1, 2)))
# Output:
The number -3 has type
Module of number -3, obtained by the abs function: 3
With type
The module of number -3, obtained by the function fabs: 3.0
With type
2.23606797749979
Traceback (most recent call last):
File "C:\Users\ivand\AppData\Roaming\JetBrains\PyCharm2021.2\scratches\scratch.py", line 12, in
print(fabs(complex(1, 2))
TypeError: can't convert complex to float
factorial() – factorial function
This function is designed to get the factorial. Example:
from math import factorial
var = 3
first_int_var = factorial(var)
print(f'The number {var} has type {type(var)}')
print(print(f'The factorial of the number {var} is of type {first_int_var}')
print('With type', type(first_int_var))
# Output:
Number 3 has type
The factorial of number 3, as calculated with the factorial function: 6
With type
Naturally, the function accepts only a positive integer.
from math import factorial
var = -3
first_int_var = factorial(var)
print(f'The number {var} has type {type(var)}')
print(print(f'The factorial of the number {var} is of type {first_int_var}')
print('With type', type(first_int_var))
# Output:
Traceback (most recent call last):
File "C:\Users\ivand\AppData\Roaming\JetBrains\PyCharm2021.2\scratches\scratch.py", line 4, in
first_int_var = factorial(var)
ValueError: factorial() not defined for negative values
from math import factorial
var = 3.14
first_int_var = factorial(var)
print(f'The number {var} has type {type(var)}')
print(f'The factorial of the number {var} obtained by the factorial function: {first_int_var}')
print('With type', type(first_int_var))
# Output:
C:\Users\ivand\AppData\Roaming\JetBrains\PyCharm2021.2\scratches\scratch.py:4: DeprecationWarning: Using factorial() with floats is deprecated
first_int_var = factorial(var)
Traceback (most recent call last):
File "C:\Users\ivand\AppData\Roaming\JetBrains\PyCharm2021.2\scratches\scratch.py", line 4, in
first_int_var = factorial(var)
ValueError: factorial() only accepts integral values
Function fmod() – the remainder of division
Function fmod() is an extension of operator % – unlike it, this function can work with floating-point numbers. Example:
from math import fmod
var = 3.14
print('fmod(var, 2)', fmod(var, 2))
print('fmod(2, var)', fmod(2, var))
print('fmod(var, 1)', fmod(var, 1))
print('fmod(var, 3.14)', fmod(var, 3.14))
print('fmod(var, 50)', fmod(var, 50))
# Output:
fmod(var, 2) 1.14000000000000000001
fmod(2, var) 2.0
fmod(var, 1) 0.14000000000000012
fmod(var, 3.14) 0.0
fmod(var, 50) 3.14
Function frexp()
This function returns the mantissa and exponent. Example:
from math import frexp
var = 3.14
print('frexp(var)', frexp(var))
# Output:
frexp(var) (0.785, 2)
Function fsum() – exact sum of float
Calculates the exact sum of the float values in the iterated object and the sum of a list or range of data. Example:
from math import fsum
var_list = [1/i for i in range(1, 10)]
print(f'The sum of the elements of the sequence\n{var_list}\ is right', fsum(var_list))
# Output:
Sum of the elements of the sequence
[1.0, 0.5, 0.3333333333333333, 0.25, 0.2, 0.16666666666666666, 0.14285714285714285, 0.125, 0.1111111111111111]
equals 2.8289689253968254
Exponentiation and logarithm functions
Function exp()
This function takes one parameter as a fractional number and returns e^x. Example:
from math import exp
var_list = 3.14
print(f'exp(3.14):', exp(var_list))
# Output:
exp(3.14): 23.103866858722185
Function expm1()
This function works the same way as exp, but returns exp(x)-1. Here, expm1 means exm-m-1, that is, exp-minus-1. Example:
from math import exp, expm1
var_list = 3.14
print(f'exp(3.14) - 1:', exp(var_list) - 1)
print(f'expm1(3.14):', expm1(var_list))
# Output:
exp(3.14) - 1: 22.103866858722185
expm1(3.14): 22.103866858722185
Function log() is the logarithm of a number
Function log(x[,base]) finds the logarithm of number x on base e (by default). the base parameter is optional. If you want to calculate the logarithm with a particular base, you must specify it. Example:
from math import log
var_list = 3.14
print(f'log(3.14):', log(var_list))
# Output:
log(3.14): 1.144222799920162
Function log1p()
This function is similar to the logarithm function, but adds 1 to x. log1p means log-1-p, that is, log-1-plus. Example:
from math import log, log1p
var_list = 3.14
print(f'log(3.14 + 1):', log(var_list + 1))
print(f'log1p(3.14):', log1p(var_list))
# Output:
log(3.14 + 1): 1.420695787837223
log1p(3.14): 1.420695787837223
Function log10()
Calculates the logarithm on base 10. Example:
from math import log10
var_list = 3.14
print(f'log10(3.14):', log10(var_list))
# Output:
log10(3.14): 0.49692964807321494
The pow() function is the power of a number
Used to find the degree of a number. The syntax of the pow(Base, Power) function. It takes two arguments: base and power. Example:
from math import pow
var_list = 3.14
print(f'pow(3.14, 10):', pow(var_list, 10))
print(f'pow(10, 3.14):', pow(10, var_list))
print(f'pow(10, 10):', pow(10, 10))
# Output:
pow(3.14, 10): 93174.3733866435
pow(10, 3.14): 1380.3842646028852
pow(10, 10): 10000000000.0
Function sqrt() – the square root of a number
This function is used to find the square root of a number. It takes a number as an argument and finds its square root. Example:
from math import sqrt
var_list = 3.14
print(f'sqrt(3.14):', sqrt(var_list))
print(f'sqrt(93174.3733866435):', sqrt(93174.3733866435))
print(f'sqrt(10000000000):', sqrt(10000000000))
# Output:
sqrt(3.14): 1.772004514666935
sqrt(93174.3733866435): 305.2447761824001
sqrt(10000000000): 100000.0
Trigonometric Functions
Python has the following trigonometric functions. Function Value sin takes a radian and returns its sine cos takes a radian and returns its cosine tan takes a radian and returns its tangent asin takes one parameter and returns arcsine (inverse sine) acos takes one parameter and returns arccosine (inverse cosine) atan takes one parameter and returns arctangent (inverse tangent) sinh takes one parameter and returns hyperbolic sine cosh takes one parameter and returns hyperbolic cosine tanh takes one parameter and returns hyperbolic tangent asinh takes one parameter and returns inverse hyperbolic sine acosh takes one parameter and returns inverse hyperbolic cosine atanh takes one parameter and returns inverse hyperbolic tangent Example:
from math import sin, cos, tan, acos, atan, sinh, acosh, atanh, asin
var = 0.5
print('sin(var):', sin(var))
print('cos(var):', cos(var))
print('tan(var):', tan(var))
print('acos(var):', acos(var))
print('asin(var):', asin(var))
print('atan(var):', atan(var))
print('sinh(var):', sinh(var))
print('acosh(3.14):', acosh(3.14))
print('atanh(var):', atanh(var))
# Output:
sin(var): 0.479425538604203
cos(var): 0.8775825618903728
tan(var): 0.5463024898437905
acos(var): 1.0471975511965979
asin(var): 0.5235987755982989
atan(var): 0.4636476090008061
sinh(var): 0.5210953054937474
acosh(3.14): 1.810991348900196
atanh(var): 0.5493061443340549
Angle Conversion Function
These functions convert an angle. In mathematics, angles can be written in two ways: angle and radian. There are two functions in Python that convert degrees to radians and back. – degrees(): converts radians to degrees; – radians(): converts degrees to radians; Example:
from math import degrees, radians
var_1 = 3.14
var_2 = 4.13
print('degrees(var_1):', degrees(var_1))
print('radians(var_2):', radians(var_2))
# Output:
degrees(var_1): 179.9087476710785
radians(var_2): 0.07208209810736581
Mathematical constants
Python has two mathematical constants: pi and e. 1. pi: is a mathematical constant with a value of 3.1416. 2. e: is a mathematical constant with a value of 2.7183. Example:
import math
# print PI value
print("PI value", math.pi)
# print("value of e
print("value of e", math.e)
Output:
pI value 3.141592653589793
value of e is 2.718281828459045