Python Solutions
1. Average and Grade Calculation
def average_and_grade(marks):
avg = sum(marks) / len(marks)
if avg >= 90:
grade = 'A'
elif avg >= 80:
grade = 'B'
elif avg >= 70:
grade = 'C'
elif avg >= 60:
grade = 'D'
else:
grade = 'F'
return avg, grade
2. Sale Price Calculation
def sale_price(cost, discount):
return cost - (cost * discount / 100)
3. Perimeter and Area Calculation
def perimeter_area_triangle(a, b, c, height):
perimeter = a + b + c
area = 0.5 * b * height
return perimeter, area
def perimeter_area_rectangle(length, width):
perimeter = 2 * (length + width)
area = length * width
return perimeter, area
def perimeter_area_square(side):
perimeter = 4 * side
area = side ** 2
return perimeter, area
def circumference_area_circle(radius):
circumference = 2 * math.pi * radius
area = math.pi * radius ** 2
return circumference, area
4. Simple and Compound Interest
def simple_interest(p, r, t):
return (p * r * t) / 100
def compound_interest(p, r, t):
return p * ((1 + (r / 100)) ** t) - p
5. Profit or Loss Calculation
def profit_loss(cost, sell_price):
if sell_price > cost:
return "Profit", sell_price - cost
elif sell_price < cost:
return "Loss", cost - sell_price
else:
return "No Profit, No Loss"
6. EMI Calculation
def emi(amount, rate, period):
rate = rate / (12 * 100) # monthly interest rate
period = period * 12 # number of monthly installments
emi_value = (amount * rate * ((1 + rate) ** period)) / (((1 + rate) ** period) - 1)
return emi_value
7. GST and Income Tax Calculation
def gst(cost, gst_rate):
return cost + (cost * gst_rate / 100)
def income_tax(income):
if income <= 250000:
return 0
elif income <= 500000:
return (income - 250000) * 0.05
elif income <= 1000000:
return 12500 + (income - 500000) * 0.2
else:
return 112500 + (income - 1000000) * 0.3
8. Largest and Smallest Number in List
def largest_smallest(nums):
return max(nums), min(nums)
9. Third Largest and Smallest Number in List
def third_largest_smallest(nums):
nums_sorted = sorted(set(nums))
return nums_sorted[-3], nums_sorted[2]
10. Sum of Squares of First 100 Natural Numbers
def sum_of_squares_100():
return sum(i**2 for i in range(1, 101))
11. First n Multiples of a Given Number
def multiples_of_number(n, number):
return [number * i for i in range(1, n+1)]
12. Count Vowels in a User-entered String
def count_vowels(string):
vowels = 'aeiouAEIOU'
return sum(1 for char in string if char in vowels)
13. Words Starting with a Given Alphabet
def words_starting_with(string, alphabet):
return [word for word in string.split() if word.startswith(alphabet)]
14. Number of Occurrences of a Given Alphabet in Each String
def occurrences_of_alphabet(string, alphabet):
return string.count(alphabet)
15. Dictionary of States and Capitals
def state_capitals():
return {
"Uttar Pradesh": "Lucknow",
"Maharashtra": "Mumbai",
"Karnataka": "Bengaluru",
"Bihar": "Patna",
"Punjab": "Chandigarh"
}
16. Dictionary of Students to Store Names and Marks
def student_marks():
return {
"John": [85, 92, 78, 88, 76],
"Alice": [90, 80, 85, 95, 88],
"Bob": [70, 75, 80, 85, 90]
}
17. Highest and Lowest Values in Dictionary
def highest_lowest_in_dict(data_dict):
highest = max(data_dict.values())
lowest = min(data_dict.values())
return highest, lowest