Numpy Python Mini projects: Expense Tracker
Expense tracker is one of the important applications in Numpy. It tracks the daily expenses, calculates the highest, lowest, average and total expenses.
Let us implement
the expense tracker in numpy.
Implementation:
- First, built-in numpy library is included. It creates an array to read the input from the user.
- Print the array of elements.
- Next, total expenses are calculated by adding all elements in the array using sum() function.
- To find the ‘Average’ value by using mean() function in the numpy package.
- Other functions are included as follows.
- ‘max()’: it gives you the maximum value.
- ‘min()’: it finds the minimum value.
- It also checks the expense value is greater than 150 and calculates the sum and prints it.
Code:
import numpy as np1
# Let us create daily expenses
s_expenses = np1.array(list(map(int, input("Enter the
weekly expenses separated by space: ").split())))
print("Expenses of the week:", s_expenses)
# Total expense calculation
total = np1.sum(s_expenses)
print("\nTotal Expense of the week: ₹", total)
# Find the Average expense
average = np1.mean(s_expenses)
print("Average Expense of the week: ₹", average)
# Maximum expense calculation
maximum = np1.max(s_expenses)
print("Highest Expense of the week: ₹", maximum)
# Minimum expense calculation
minimum = np1.min(s_expenses)
print("Lowest Expense of the week: ₹", minimum)
# Expenses greater than ₹150
high_expenses = s_expenses[s_expenses > 150]
print("\nExpenses Above ₹150:", high_expenses)
# Number of days with expenses above ₹150
count = np1.sum(s_expenses > 150)
print("Days Above ₹150:", count)
Output:
Enter the weekly expenses separated by space: 120 210 180
300 75 160 250 90
Expenses of the week: [120 210 180 300 75 160 250
90]
Total Expense of the week: ₹ 1385
Average Expense of the week: ₹ 173.125
Highest Expense of the week: ₹ 300
Lowest Expense of the week: ₹ 75
Expenses Above ₹150: [210 180 300 160 250]
Days Above ₹150: 5
That’s all.
The Python Numpy program to implement the expense tracker was done
successfully. Keep doing!!!
Comments
Post a Comment