Python is a powerful programming language that offers a wide range of modules to extend its functionality. In this tutorial, we will explore some commonly used modules that beginners are likely to encounter early on in their Python journey. We will cover the math, random, and statistics modules, explaining their purpose and providing simple examples to demonstrate their usage.
This module provides access to the mathematical functions. All return values of mathematical functions are floats. The Math Library not only has functions but also useful constants like π and e. To use the functions or the constants in your program you must apply the dot operator. The general syntax for usage is math.function() or math.constant.
Some of the functions and constants of math modules are:
math.sqrt(x) | Return the square root of x. |
math.ceil(x) | Return the ceiling of x, the smallest integer greater than or equal to x. |
math.floor(x) | Return the floor of x, the largest integer less than or equal to x. |
math.pow(x,y) | Return x raised to the power y. Unlike the built-in ** operator, math.pow() converts both its arguments to type float. Use ** or the built-in pow() function for computing exact integer powers. |
math.fabs(x) | Return the absolute value of x. |
math.radians(x) | Return the radians of x degree. |
math.sin(x) | Return the sine of x radians. |
math.cos(x) | Return the cosine of x radians. |
math.tan(x) | Return the tangent of x radians. |
math.pi | The mathematical constant π = 3.141592…. |
math.e | The mathematical constant e = 2.718281… |
Example
Program to find the distance between two points. The distance between two points whose starting and ending points A(x1, y1) and B(x2, y2) is
Program
import math
x1, y1 = (2,3)
x2, y2 = (0,0)
d = math.sqrt(math.pow((x2 - x1), 2) + math.pow((y2 - y1), 2))
print('The distance between A and B is', d)
Output
The distance between A and B is 3.605551275463989
Example
Create a program that displays the sine, cosine, and tangent values of angle 45 degree.
import math
angle_degrees = 45
angle_radians = math.radians(angle_degrees)
sin_value = math.sin(angle_radians)
cos_value = math.cos(angle_radians)
tan_value = math.tan(angle_radians)
print("Angle:", angle_degrees, "degrees")
print("Sine:", sin_value)
print("Cosine:", cos_value)
print("Tangent:", tan_value)
Output
This module implements pseudo-random number generators for integers, sequences and real numbers.
random.randint(a, b) | Return a random integer N such that a <= N <= b. |
random.randrange([start], stop[, step]) | Return a randomly selected element from range(start, stop, step). |
random.random() | Return the next random floating-point number in the range [0.0, 1.0]. |
Example of generating random integer
import random
random_integer = random.randint(1, 10)
print("Random integer between 1 and 10:", random_integer)
Output
Random integer between 1 and 10: 4
This module provides functions for calculating mathematical statistics of numeric (Real-valued) data.
statistics.mean(data) | Return the sample arithmetic mean of data which can be a sequence or iterable. |
statistics.median(data) | Return the median (middle value) of numeric data, using the common "mean of middle two" method. |
statistics.mode(data) | Return the single most common data point from discrete or nominal data. |
Example
import statistics
data = (21, 3, 7, 17, 35, 31, 46, 7, 43)
a = statistics.mean(data)
b = statistics.median(data)
c = statistics.mode(data)
print('mean =', a)
print('median =', b)
print('mode =', c)
Output
mean = 23.333333333333332 median = 21 mode = 7
In this tutorial, we explored some commonly used modules in Python. The math module allows us to perform mathematical operations, including square roots and trigonometric calculations. The random module enables us to generate random numbers and make random selections. The statistics module provides functions for statistical calculations, such as finding the mean.