{}
See how a CS professor is using our compiler for class assignment.
Try Programiz PRO for Educators!
Learn DSA with step-by-step code visualization.
Try Programiz PRO for Educators!
run-icon
main.py
# Online Python compiler (interpreter) to run Python online. # Write Python 3 code in this online editor and run it. from math import sqrt from collections import Counter # ------------------------------- # Level 2: Statistics Class # ------------------------------- class Statistics: def __init__(self, data): self.data = sorted(data) def count(self): return len(self.data) def sum(self): return sum(self.data) def min(self): return min(self.data) def max(self): return max(self.data) def range(self): return self.max() - self.min() def mean(self): return round(self.sum() / self.count()) def median(self): n = self.count() mid = n // 2 if n % 2 == 0: return (self.data[mid - 1] + self.data[mid]) / 2 else: return self.data[mid] def mode(self): freq = Counter(self.data) max_count = max(freq.values()) mode = [k for k, v in freq.items() if v == max_count] return (mode[0], max_count) if len(mode) == 1 else (mode, max_count) def var(self): mean = self.mean() return round(sum((x - mean) ** 2 for x in self.data) / self.count(), 1) def std(self): return round(sqrt(self.var()), 1) def freq_dist(self): freq = Counter(self.data) total = self.count() return sorted([(round((v / total) * 100, 1), k) for k, v in freq.items()], reverse=True) def describe(self): print('Count:', self.count()) print('Sum: ', self.sum()) print('Min: ', self.min()) print('Max: ', self.max()) print('Range: ', self.range()) print('Mean: ', self.mean()) print('Median: ', self.median()) print('Mode: ', self.mode()) print('Variance: ', self.var()) print('Standard Deviation: ', self.std()) print('Frequency Distribution:', self.freq_dist()) # ------------------------------- # Level 3: PersonAccount Class # ------------------------------- class PersonAccount: def __init__(self, firstname, lastname): self.firstname = firstname self.lastname = lastname self.incomes = {} # {description: amount} self.expenses = {} # {description: amount} def add_income(self, description, amount): self.incomes[description] = self.incomes.get(description, 0) + amount def add_expense(self, description, amount): self.expenses[description] = self.expenses.get(description, 0) + amount def total_income(self): return sum(self.incomes.values()) def total_expense(self): return sum(self.expenses.values()) def account_balance(self): return self.total_income() - self.total_expense() def account_info(self): info = f''' Name: {self.firstname} {self.lastname} Total Income: {self.total_income()} Total Expenses: {self.total_expense()} Account Balance: {self.account_balance()} Income Details: {self.incomes} Expense Details: {self.expenses} ''' return info.strip() # ------------------------------- # Example Usage # ------------------------------- # Statistics Example print("📊 Statistics Output:") ages = [31, 26, 34, 37, 27, 26, 32, 32, 26, 27, 27, 24, 32, 33, 27, 25, 26, 38, 37, 31, 34, 24, 33, 29, 26] data = Statistics(ages) data.describe() print("\n💰 Person Account Output:") # Person Account Example person = PersonAccount("Nidhi", "Agarwal") person.add_income("Salary", 5000) person.add_income("Freelance", 1200) person.add_expense("Rent", 1500) person.add_expense("Food", 600) print(person.account_info())
Output