Python Math

Introduction

Python provides several libraries and modules to perform mathematical operations. The most commonly used module is the math module, which includes functions for basic mathematical operations, trigonometry, logarithms, and more. Other useful libraries include cmath for complex numbers, random for generating random numbers, and statistics for statistical operations.

The math Module

The math module provides a wide range of mathematical functions. To use these functions, you need to import the module.

Importing the math Module

import math

Basic Mathematical Functions

Example

# Square root
print(math.sqrt(16))  # Output: 4.0

# Power
print(math.pow(2, 3))  # Output: 8.0

# Absolute value
print(math.fabs(-5))  # Output: 5.0

# Factorial
print(math.factorial(5))  # Output: 120

Trigonometric Functions

Example

# Sine
print(math.sin(math.pi / 2))  # Output: 1.0

# Cosine
print(math.cos(0))  # Output: 1.0

# Tangent
print(math.tan(math.pi / 4))  # Output: 1.0

# Converting degrees to radians
print(math.radians(180))  # Output: 3.141592653589793

# Converting radians to degrees
print(math.degrees(math.pi))  # Output: 180.0

Logarithmic Functions

Example

# Natural logarithm (base e)
print(math.log(10))  # Output: 2.302585092994046

# Logarithm with base 10
print(math.log10(100))  # Output: 2.0

# Exponential function (e^x)
print(math.exp(2))  # Output: 7.3890560989306495

Constants

Example

# Value of Pi
print(math.pi)  # Output: 3.141592653589793

# Value of Euler's number (e)
print(math.e)  # Output: 2.718281828459045

The cmath Module

The cmath module provides functions for complex numbers.

Importing the cmath Module

import cmath

Complex Number Functions

Example

# Creating a complex number
complex_num = 1 + 2j

# Square root of a complex number
print(cmath.sqrt(complex_num))  # Output: (1.272019649514069+0.7861513777574233j)

# Exponential of a complex number
print(cmath.exp(complex_num))  # Output: (-1.1312043837568135+2.4717266720048188j)

# Sine of a complex number
print(cmath.sin(complex_num))  # Output: (3.165778513216168+1.959601041421606j)

The random Module

The random module provides functions to generate random numbers and perform random operations.

Importing the random Module

import random

Random Number Functions

Example

# Random integer between 1 and 10
print(random.randint(1, 10))

# Random floating-point number between 0 and 1
print(random.random())

# Random choice from a list
choices = ['apple', 'banana', 'cherry']
print(random.choice(choices))

# Shuffling a list
random.shuffle(choices)
print(choices)

# Random sample of 2 elements from a list
print(random.sample(choices, 2))

The statistics Module

The statistics module provides functions for statistical operations.

Importing the statistics Module

import statistics

Statistical Functions

Example

data = [1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10]

# Mean
print(statistics.mean(data))  # Output: 5.0

# Median
print(statistics.median(data))  # Output: 5

# Mode
print(statistics.mode(data))  # Output: 5

# Standard deviation
print(statistics.stdev(data))  # Output: 2.6457513110645907

# Variance
print(statistics.variance(data))  # Output: 7.0

Conclusion

Python’s mathematical libraries provide powerful tools for performing a wide range of mathematical operations. The math module covers basic and advanced mathematical functions, cmath handles complex numbers, random is useful for generating random numbers and performing random operations, and statistics is essential for statistical analysis. Understanding these modules is essential for handling mathematical tasks in Python effectively.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top