Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How To Do Math With Lists in python ?
Lists in Python can be used for various mathematical operations beyond just storing values. Whether calculating weighted averages, performing trigonometric functions, or applying mathematical operations to collections of data, Python's math module combined with list operations provides powerful computational capabilities.
Basic Math Operations with Single Values
The math module provides functions for common mathematical operations ?
import math
data = 21.6
print('The floor of 21.6 is:', math.floor(data))
print('The ceiling of 21.6 is:', math.ceil(data))
print('The factorial of 5 is:', math.factorial(5))
The floor of 21.6 is: 21 The ceiling of 21.6 is: 22 The factorial of 5 is: 120
Calculating Weighted Average of Lists
A weighted average gives different importance to different values based on their weights ?
# Calculate weighted average
values = [23, 10, 5, 32, 41]
weights = [10, 20, 30, 40, 50]
weighted_sum = sum(value * weight for value, weight in zip(values, weights))
total_weight = sum(weights)
weighted_average = weighted_sum / total_weight
print(f'Weighted average: {weighted_average}')
print(f'Individual contributions: {[v * w for v, w in zip(values, weights)]}')
Weighted average: 25.9 Individual contributions: [230, 200, 150, 1280, 2050]
Trigonometric Operations with Lists
Apply trigonometric functions to multiple angles stored in lists ?
import math
degrees = [0, 30, 60, 90, 180]
radians = [math.radians(deg) for deg in degrees]
print('Angle\tSin\t\tCos\t\tTan')
print('-' * 40)
for deg, rad in zip(degrees, radians):
sin_val = round(math.sin(rad), 4)
cos_val = round(math.cos(rad), 4)
tan_val = round(math.tan(rad), 4) if abs(math.cos(rad)) > 1e-10 else 'undefined'
print(f'{deg}°\t{sin_val}\t\t{cos_val}\t\t{tan_val}')
Angle Sin Cos Tan ---------------------------------------- 0° 0.0 1.0 0.0 30° 0.5 0.866 0.577 60° 0.866 0.5 1.732 90° 1.0 0.0 undefined 180° 0.0 -1.0 -0.0
Element-wise Mathematical Operations
Perform mathematical operations on each element of a list ?
import math
numbers = [1, 4, 9, 16, 25]
# Square roots
square_roots = [math.sqrt(x) for x in numbers]
print('Square roots:', square_roots)
# Powers
powers_of_2 = [x ** 2 for x in numbers]
print('Squares:', powers_of_2)
# Logarithms
logarithms = [round(math.log(x), 2) for x in numbers]
print('Natural logarithms:', logarithms)
Square roots: [1.0, 2.0, 3.0, 4.0, 5.0] Squares: [1, 16, 81, 256, 625] Natural logarithms: [0.0, 1.39, 2.2, 2.77, 3.22]
Common Python Math Functions
| Function | Description | Example |
|---|---|---|
ceil(x) |
Smallest integer ? x |
math.ceil(4.2) ? 5 |
floor(x) |
Largest integer ? x |
math.floor(4.7) ? 4 |
fabs(x) |
Absolute value |
math.fabs(-5) ? 5.0 |
factorial(x) |
Factorial of x |
math.factorial(4) ? 24 |
sqrt(x) |
Square root |
math.sqrt(16) ? 4.0 |
pow(x, y) |
x raised to power y |
math.pow(2, 3) ? 8.0 |
Conclusion
Python's math module combined with list comprehensions enables efficient mathematical operations on collections of data. Use list comprehensions for element-wise operations and built-in functions like sum() and zip() for calculations across multiple lists.
