Basic Functions for Analyzing Numbers in Python: How to Use abs, sum, max, and min

When performing data analysis or numerical calculations, you often need to find basic metrics like absolute values, sums, maximums, and minimums to understand the “magnitude” or “trends” of your data.

Python provides convenient built-in functions to perform these calculations.

This article explains how to use four of these functions: abs(), sum(), max(), and min(), along with specific usage scenarios.

目次

1. Finding the Absolute Value: abs()

The abs() function returns the absolute value of a number (its distance from 0). If the argument is positive, it returns it as is; if it is negative, it converts it to a positive number.

Syntax:

absolute_value = abs(number)

Specific Usage Example: Calculating Error

When calculating the gap (error) between a predicted value and an actual value, simple subtraction might result in a negative number. If you only want to know the magnitude of the gap, use the absolute value.

# Predicted and actual values
predicted_val = 150
actual_val = 135

# Difference (simple subtraction)
diff = actual_val - predicted_val
print(f"Simple difference: {diff}")

# Error margin (absolute value)
error_margin = abs(diff)
print(f"Error magnitude: {error_margin}")

Execution Result:

Simple difference: -15
Error magnitude: 15

2. Finding the Sum: sum()

The sum() function takes an iterable (a collection of numbers, such as a list or tuple) and returns the total sum of its elements.

Syntax:

total = sum(iterable_like_list)

Specific Usage Example: Aggregating Weekly Sales

Here is an example of calculating total sales from a list of weekly sales data.

# Weekly sales data (in thousands)
weekly_sales = [120, 150, 90, 200, 180, 250, 300]

# Calculate total
total_sales = sum(weekly_sales)

print(f"Weekly sales data: {weekly_sales}")
print(f"Total sales: {total_sales} thousand")

Execution Result:

Weekly sales data: [120, 150, 90, 200, 180, 250, 300]
Total sales: 1290 thousand

3. Finding Maximum and Minimum Values: max(), min()

The max() function returns the largest element, and the min() function returns the smallest element. You can pass a list as an argument or multiple numbers directly.

Syntax:

maximum = max(list_or_numbers)
minimum = min(list_or_numbers)

Specific Usage Example: High and Low Temperatures

This example extracts the highest and lowest temperatures from a set of temperature data.

# Temperature data (Celsius)
temperatures = [18.5, 22.0, 16.8, 25.4, 19.1]

# Get max and min values
max_temp = max(temperatures)
min_temp = min(temperatures)

print(f"Observation data: {temperatures}")
print(f"Highest temperature: {max_temp} degrees")
print(f"Lowest temperature: {min_temp} degrees")

Execution Result:

Observation data: [18.5, 22.0, 16.8, 25.4, 19.1]
Highest temperature: 25.4 degrees
Lowest temperature: 16.8 degrees

Application: Combining These Functions

By combining these functions, you can easily calculate statistical metrics like “average” and “range.”

# Calculate average (sum / number of elements)
average_temp = sum(temperatures) / len(temperatures)

# Calculate range (max - min)
temp_range = max(temperatures) - min(temperatures)

print(f"Average temperature: {average_temp:.1f} degrees")
print(f"Temperature range: {temp_range:.1f} degrees")

Execution Result:

Average temperature: 20.4 degrees
Temperature range: 8.6 degrees

Summary

Python includes a standard set of basic functions for handling numerical data.

  • abs(x): Finds the absolute value (converts negative numbers to positive).
  • sum(iterable): Finds the sum of a list or other iterable.
  • max(iterable): Finds the maximum value.
  • min(iterable): Finds the minimum value.

These are important functions that form the basis of data analysis and algorithm implementation.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次