0% found this document useful (0 votes)
7 views

Week Lab

The document discusses calculating statistics like mean, median, mode and quartiles from a dataset. It includes code to define functions for calculating each statistic and applying them to sample data. It also discusses creating a set from input elements, calculating its power set and checking if an element belongs to the set.

Uploaded by

Aff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Week Lab

The document discusses calculating statistics like mean, median, mode and quartiles from a dataset. It includes code to define functions for calculating each statistic and applying them to sample data. It also discusses creating a set from input elements, calculating its power set and checking if an element belongs to the set.

Uploaded by

Aff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Week 3&4

2# Suppose that the data for analysis includes the attribute age. The age values for the
data tuples are (in increasing order) 13, 15, 16, 16, 19, 20, 20, 21, 22, 22, 25, 25, 25,
25, 30, 33, 33, 35, 35, 35, 35, 36, 40, 45, 46, 52, 70.
(a) Calculate the mean.
(b) Calculate the median.
(c) Calculate the mode.
Calculate first quartile (Q1) and the third quartile (Q3).

def calculate_mean(numbers):
total = sum(numbers)
mean = total / len(numbers)
return mean

def calculate_median(numbers):
sorted_numbers = sorted(numbers)
n = len(sorted_numbers)
if n % 2 == 0:
median = (sorted_numbers[n//2 - 1] + sorted_numbers[n//2]) / 2
else:
median = sorted_numbers[n//2]
return median

def calculate_mode(numbers):
counts = {}
for num in numbers:
if num in counts:
counts[num] += 1
else:
counts[num] = 1
max_count = max(counts.values())
mode = [num for num, count in counts.items() if count == max_count]
return mode

def calculate_quartiles(numbers):
sorted_numbers = sorted(numbers)
n = len(sorted_numbers)
if n % 2 == 0:
lower_half = sorted_numbers[:n//2]
upper_half = sorted_numbers[n//2:]
else:
lower_half = sorted_numbers[:n//2]
upper_half = sorted_numbers[n//2 + 1:]
rst_quartile = calculate_median(lower_half)
third_quartile = calculate_median(upper_half)
return [ rst_quartile, third_quartile]

# Example usage:
data = [1, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9]
# data = input("Enter numbers separated by a space: ").split()
mean = calculate_mean(data)
median = calculate_median(data)
mode = calculate_mode(data)
quartiles = calculate_quartiles(data)
print("Mean:", mean)
print("Median:", median)
print("Mode:", mode)
print("Quartiles:", quartiles)
fi
fi
OUTPUT

3# Write a program to create a SET A and determine the cardinality of SET for an input
Array of elements
(repetition allowed) and perform the following operations on the SET:
a) ismember (a, A): check whether an element belongs to a set or not and return the
value as true/false.
b) powerset(A): list all the elements of the power set of A.

def ismember(a, A):


return a in A

def powerset(A):
power_set = [[]]
for element in A:
subsets = [subset + [element] for subset in power_set]
power_set.extend(subsets)
return power_set

A = set(input("Enter the elements of the set (separated by spaces): ").split())

power_set = powerset(A)
print("Power set of the set:", power_set)

cardinality = len(A)
print("Cardinality of the set:", cardinality)

element = input("Enter an element to check if it belongs to the set: ")


if ismember(element, A):
print(element, "belongs to the set")
else:
print(element, "does not belong to the set")

OUTPUT

You might also like