Source Of The Section

Sec2_DM_Spring2025.pdf

Functions

Function to Calculate the Average of a List of Numbers

def calculate_average(numbers):
    return sum(numbers) / len(numbers)

# Call the function with a list of numbers
calculate_average([10, 20, 30, 40])  # Output: 25.0
calculate_average([1, 2, 3, 4, 5])   # Output: 3.0

This function calculates the average of a given list of numbers by summing all values and dividing by the length of the list.

Function to Find the Second Largest Number in a List

def second_largest(numbers):
    unique_numbers = list(set(numbers))  # Remove duplicates
    unique_numbers.sort()  # Sort the list
    return unique_numbers[-2]  # Return the second largest number

# Example usage
second_largest([10, 20, 30, 40])  # Output: 30
second_largest([5, 1, 9, 7])      # Output: 7

This function finds the second largest number in a given list. It removes duplicates, sorts the list, and returns the second last element.


Data Visualization

Data visualization is the graphical representation of information and data using visual elements like charts, graphs, and maps.


Comparison Table for Different Chart Types

Chart Type Purpose Best Used For Key Characteristics Example Code
Line Plot Shows trends over time Stock prices, temperature changes X-axis represents time or continuous data, Y-axis represents measured value plt.plot(x, y)
Bar Plot Compares different categories Sales by product, population by country Bars can be vertical/horizontal, height represents value plt.bar(categories, values)
Histogram Displays frequency distribution Exam scores, customer ages Groups data into bins, shows frequency plt.hist(data, bins=10)
Box Plot Identifies outliers Income analysis, detecting fraud Shows min, max, median, and outliers plt.boxplot(data)
Scatter Plot Shows relationships between variables Study time vs. exam score Each point represents an observation plt.scatter(x, y)
Pie Chart Shows parts of a whole Market share, budget allocation Circular chart, total sum = 100% plt.pie(data, labels)

Libraries Required

To install the necessary library:

pip install matplotlib

Importing Libraries