4. Built-In Modules#

Table of Built-In Modules#

Module

Description

math

Provides basic mathematical functions like sqrt, sin, and cos, as well as constants like pi and e.

cmath

Provides basic complex mathematical functions to deal with complex number

random

Offers functionality for generating random numbers, shuffling sequences, and picking random items from lists.

os

Enables interaction with the operating system, such as working with files, directories, and environment variables.

sys

Gives access to system-specific parameters and functions, including command-line arguments and the Python runtime.

time

Provides time-related functions, like getting the current time, pausing execution (sleep), or measuring performance.

datetime

Supplies classes for manipulating dates and times, including formatting and arithmetic with time objects.

re

Implements regular expression operations for string searching, matching, and substitution.

json

Lets you read and write JSON (JavaScript Object Notation) data for easy data serialization and sharing.

csv

Simplifies reading and writing CSV (Comma-Separated Values) files.

Statistcs

Statistics methods and functions.

Math Module#

Overview#

The math module is a built-in module that comes with your installation of python. When you start Python it is not loaded into memory and you need to import it before you can use it with the statement:

import math

Table of Common Math functions#

Method/Constant

Description

math.ceil(x)

Returns the smallest integer greater than or equal to x

math.floor(x)

Returns the largest integer less than or equal to x

math.fabs(x)

Returns the absolute value of x

math.factorial(x)

Returns the factorial of x

math.gcd(a, b)

Returns the greatest common divisor of a and b

math.exp(x)

Returns e raised to the power of x

math.log(x[, base])

Returns the logarithm of x to the given base (default is e)

math.log10(x)

Returns the base-10 logarithm of x

math.pow(x, y)

Returns x raised to the power of y

math.sqrt(x)

Returns the square root of x

math.sin(x)

Returns the sine of x (x in radians)

math.cos(x)

Returns the cosine of x (x in radians)

math.tan(x)

Returns the tangent of x (x in radians)

math.degrees(x)

Converts angle x from radians to degrees

math.radians(x)

Converts angle x from degrees to radians

math.pi

Mathematical constant pi (3.141592…)

math.e

Mathematical constant e (2.718281…)

Random Module#

Function/Method

Description

Example

random.random()

Returns a random float between 0 and 1.

random.random() 0.6734

random.uniform(a, b)

Returns a random float between a and b.

random.uniform(10, 20) 15.738

random.randint(a, b)

Returns a random integer between a and b (inclusive).

random.randint(1, 6) 4

random.randrange(start, stop, step)

Returns a random integer from range(start, stop, step).

random.randrange(0, 10, 2) 4

random.choice(seq)

Returns a random element from a non-empty sequence.

random.choice(['H', 'He', 'Li']) 'Li'

random.choices(seq, k=n)

Returns n random elements from seq, with replacement.

random.choices(['H', 'He', 'Li'], k=2) ['He', 'Li']

random.sample(seq, k=n)

Returns n unique random elements from seq (no replacement).

random.sample(range(10), k=3) [2, 7, 5]

random.shuffle(seq)

Shuffles a list in place.

random.shuffle(my_list)

random.seed(n)

Initializes the random number generator for reproducibility.

random.seed(42)

random.gauss(mu, sigma)

Generates a random number from a normal distribution with mean mu and standard deviation sigma.

random.gauss(0, 1) 0.456

random.expovariate(lambd)

Returns a random number from an exponential distribution with rate parameter lambd.

random.expovariate(1.5) 0.762

random.betavariate(alpha, beta)

Returns a random number from a Beta distribution.

random.betavariate(2, 5) 0.23

import random

# Seeding for reproducibility
random.seed(42)

# Generate random float
print("Random float (0-1):", random.random())

# Generate random integer
print("Random integer (1-10):", random.randint(1, 10))

# Generate a random float in a range
print("Random float (10-20):", random.uniform(10, 20))

# Random choice from a list
elements = ['H', 'He', 'Li', 'Be', 'B']
print("Random element:", random.choice(elements))

# Random sample (without replacement)
print("Random sample of 3 elements:", random.sample(elements, 3))

# Shuffle a list
random.shuffle(elements)
print("Shuffled elements:", elements)

# Normal distribution
print("Random number from normal distribution (mean=0, std=1):", random.gauss(0, 1))
Random float (0-1): 0.6394267984578837
Random integer (1-10): 1
Random float (10-20): 17.41550499759833
Random element: He
Random sample of 3 elements: ['He', 'B', 'Li']
Shuffled elements: ['Be', 'He', 'Li', 'B', 'H']
Random number from normal distribution (mean=0, std=1): 0.4347603342594289

Statistics Module#

Overview#

The statistics module is part of Pythons standard (built-in) library and is useful for small data sets. We will be using Numpy and Pandas for larger data sets. But first things first.

Table Statistics Methods#

Function Name

Description

statistics.mean(data)

Returns the arithmetic mean of a numeric dataset.

statistics.median(data)

Returns the median (middle value) of numeric data.

statistics.median_low(data)

Returns the lower median of numeric data.

statistics.median_high(data)

Returns the higher median of numeric data.

statistics.mode(data)

Returns the most common value in a dataset.

statistics.multimode(data)

Returns a list of the most frequently occurring values.

statistics.variance(data, xbar=None)

Returns the sample variance of a dataset.

statistics.stdev(data, xbar=None)

Returns the sample standard deviation.

statistics.pvariance(data, mu=None)

Returns the population variance.

statistics.pstdev(data, mu=None)

Returns the population standard deviation.

statistics.fmean(data)

Returns the arithmetic mean as a floating-point number (faster than mean()).

statistics.geometric_mean(data)

Returns the geometric mean of positive numbers.

statistics.harmonic_mean(data)

Returns the harmonic mean of positive numbers.

statistics.quantiles(data, n=4, method='exclusive')

Divides data into equal intervals (default is quartiles).

statistics.correlation(x, y)

Computes Pearson’s correlation coefficient between two datasets.

statistics.covariance(x, y)

Computes the covariance between two datasets.

statistics.linear_regression(x, y)

Computes slope and intercept of simple linear regression.

statistics.normalvariate(mu, sigma)

Returns a random float from a normal distribution.

statistics.gauss(mu, sigma)

Generates a random number based on the Gaussian distribution.

Examples#

Create Normally Distributed Data Set#

The following code will generate 100 datapoints with a random gaussian distribution centered around 50 and plot it using matplotlib

import random
import matplotlib.pyplot as plt

# Set seed for reproducibility
random.seed(32)

# Generate 100 data points with a normal distribution (mean = 50, stdev = 10)
data = [random.gauss(50, 10) for _ in range(100)]

# Optional: Round values for simpler interpretation
data = [round(num, 2) for num in data]

# Preview first 10 values
print(data[:10])
plt.figure(figsize=(8, 5))
plt.hist(data, bins=10, edgecolor='black')
plt.title('Histogram of Normally Distributed Data')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.grid(True)
plt.show()
[56.13, 53.24, 42.97, 70.28, 34.04, 50.38, 59.64, 57.02, 56.71, 39.86]
../../_images/cbb59dbdea62593602e89041c38b5f7a6b6ea16290fa88194a1ec3e926ead98a.png

Mean (Arithmetic Average)#

The mean is the sum of all numbers in the dataset divided by the number of values. It gives a sense of the “typical” value.

import statistics

mean_value = statistics.mean(data)
print(f"Mean: {mean_value}")
Mean: 49.4651

Median (Middle Value)#

The median is the middle value in a sorted list. If there’s an even number of values, it’s the average of the two middle ones.

median_value = statistics.median(data)
print(f"Median: {median_value}")
Median: 50.33

Mode (Most Frequent Value)#

The mode is the most commonly occurring value in the dataset. A dataset can have more than one mode (bimodal, multimodal).

mode_value = statistics.mode(data)
print(f"Mode: {mode_value}")
Mode: 52.66

Multimode (All Most Frequent Values)#

Multimode returns a list of all values that appear with the highest frequency. Useful if there are ties. This returns a list

multimode_values = statistics.multimode(data)
print(f"Multimode: {multimode_values}")
print(type(multimode_values))
Multimode: [52.66]
<class 'list'>

Variance (Sample)#

Sample variance measures how spread out the data is from the mean. Higher variance means more spread.

sample_variance = statistics.variance(data)
print(f"Sample Variance: {sample_variance}")
Sample Variance: 112.42942120202021

Standard Deviation (Sample)#

Standard deviation is the square root of variance. It tells us the typical distance of values from the mean.

sample_stdev = statistics.stdev(data)
print(f"Sample Standard Deviation: {sample_stdev}")
Sample Standard Deviation: 10.603274079359648

Population Variance & Standard Deviation#

If your dataset represents the entire population (not a sample), use pvariance() and pstdev()

population_variance = statistics.pvariance(data)
population_stdev = statistics.pstdev(data)

print(f"Population Variance: {population_variance}")
print(f"Population Standard Deviation: {population_stdev}")
Population Variance: 111.30512699
Population Standard Deviation: 10.550124501161113

Quantiles#

Quantiles divide your data into equal-sized intervals. For example, quartiles divide the data into 4 parts.

quartiles = statistics.quantiles(data, n=4)
print(f"Quartiles: {quartiles}")
Quartiles: [43.53, 50.33, 54.805]

Geometric Mean#

The geometric mean is used for data that multiplies over time (e.g., growth rates). It’s the nth root of the product of the data

geo_mean = statistics.geometric_mean(data)
print(f"Geometric Mean: {geo_mean}")
Geometric Mean: 48.25233824320789

Correlation and Covariance (Optional Intro)#

Correlation shows how two variables move together. +1 is perfect positive, -1 is perfect negative, 0 is no relation.

# Generate a second variable (y) that tracks x with some noise
x = list(range(25))
y = [d + random.randint(-5, 5) for d in data]

corr = statistics.correlation(data, y)
cov = statistics.covariance(data, y)

print(f"Correlation: {corr}")
print(f"Covariance: {cov}")
Correlation: 0.9514448415358394
Covariance: 113.6918302929293

Complex Math cmath Module#

Overview#

  • Purpose: Perform mathematical operations on complex numbers, such as trigonometric, logarithmic, and exponential functions.

  • Key Difference from math: While the math module raises errors for invalid operations on real numbers (e.g., square root of a negative number), cmath seamlessly handles such cases by returning a complex result.


Table of Common cmath Functions#

Below is a table of common functions available in the cmath module that are not directly accessible with standard Python operations:

Function

Description

cmath.sqrt(z)

Returns the square root of the complex number z.

cmath.exp(z)

Returns the exponential of the complex number z (e**z).

cmath.log(z, base)

Returns the natural logarithm (base e) of z. Optionally, you can specify a different base.

cmath.log10(z)

Returns the base-10 logarithm of z.

cmath.phase(z)

Returns the phase (angle in radians) of z.

cmath.polar(z)

Returns a tuple (r, phi) where r is the magnitude and phi is the phase of z in polar form.

cmath.rect(r, phi)

Converts polar coordinates (r, phi) to a complex number in rectangular form.

cmath.sin(z)

Returns the sine of z.

cmath.cos(z)

Returns the cosine of z.

cmath.tan(z)

Returns the tangent of z.

cmath.asin(z)

Returns the inverse sine of z.

cmath.acos(z)

Returns the inverse cosine of z.

cmath.atan(z)

Returns the inverse tangent of z.

cmath.sinh(z)

Returns the hyperbolic sine of z.

cmath.cosh(z)

Returns the hyperbolic cosine of z.

cmath.tanh(z)

Returns the hyperbolic tangent of z.


Acknowledgements#

This content was developed with assistance from Perplexity AI and Chat GPT. Multiple queries were made during the Fall 2024 and the Spring 2025.