Python Programs: Calculating Compound Interest and Distance Between Two Points

Aim: i) Write a program to calculate compound interest when principal, rate and number of periods are given.

Program:

# Python program to compute compound interest
p = float(input("Enter the principal amount : "))
t = float(input("Enter the number of years : "))
r = float(input("Enter the rate of interest : "))

# compute compound interest
ci = p * (pow((1 + r / 100), t))

# print
print("Compound interest : {}".format(ci))
Output:

Enter the principal amount : 25000
Enter the number of years : 2
Enter the rate of interest : 5
Compound interest : 27562.5

Aim: ii)Given coordinates (x1, y1), (x2, y2) find the distance between two points

Program:

# Python Program to Calculate Distance 

# Reading co-ordinates
x1 = float(input('Enter x1: '))
y1 = float(input('Enter y1: '))
x2 = float(input('Enter x2: '))
y2 = float(input('Enter y2: '))

# calculating distance
d = ((x2 - x1)**2 + (y2 - y1)**2) ** 0.5

# Displaying result
print('Distance = %f' % (d))
Output:

Enter x1: 5
Enter y1: 4
Enter x2: 3
Enter y2: 2
Distance = 2.828427

Sample Viva Questions and Answers

1. What is compound interest?
Compound interest is the interest calculated on the initial principal and also on the accumulated interest from previous periods.

2. What inputs does the compound interest program require?
The principal amount, the rate of interest, and the number of years.

3. How does the program handle invalid numerical inputs?
The program currently does not handle invalid inputs, which may raise an error.

4. What is the purpose of the powย function in the compound interest calculation?
It calculates the power of a number, specifically for compounding interest.

5. What inputs are needed for the distance calculation program?
The coordinates of two points: (x1, y1) and (x2, y2).

6. What mathematical concept is used in the distance formula?
The Pythagorean theorem.

7. How would you modify the distance program for three-dimensional coordinates?
By adding a third coordinate (z1, z2) and updating the distance formula accordingly.

8. What would the output be if the principal amount is zero in the compound interest program?
The output would be zero.

9. How can floating-point arithmetic affect the results?
It can introduce precision errors, leading to slight inaccuracies in calculations.

10. What improvements could be made to these programs?
Adding input validation, error handling, and support for different compounding periods or 3D coordinates.

๐Ÿ“Œ Python Programming Lab – Complete Guide

Welcome to the Python Programming Lab! This comprehensive lab series is designed to provide a solid foundation in Python programming through practical exercises and hands-on learning. From basic calculations to advanced concepts like file handling, modules, and GUI programming, this lab covers it all. Letโ€™s dive into what youโ€™ll be learning throughout this lab course.


๐Ÿ” Week 1: Introduction to Python Basics

In the first week, you will explore fundamental Python concepts aimed at building a strong foundation for your programming journey.

  • Getting Started with Python: Visit the Python Official Website to explore documentation and use the help() function in the interpreter.
  • Python as a Calculator: Learn how to perform basic arithmetic operations like addition, subtraction, multiplication, and division.
  • Calculating Compound Interest: Write a program to calculate compound interest using the formula with given principal, rate, and time.
  • Distance Calculation: Compute the distance between two points using the distance formula.
  • Reading User Details: Write a program to collect and print user details like name, address, email, and phone number.

๐Ÿ” Week 2: Python Loops and Conditional Statements

This week focuses on using loops and conditional statements effectively.

  • Pattern Generation: Print a triangle pattern using nested loops.
  • Character Identification: Write a program to identify whether the input is a digit, lowercase, uppercase, or special character.
  • Fibonacci Sequence Generation: Use a while loop to generate the Fibonacci series.
  • Prime Numbers Identification: Find all prime numbers within a specified range using the break statement.

๐Ÿ” Week 3: Data Structures and Functions

Dive into data structures and creating functions to enhance your programming skills.

  • Converting Lists and Tuples to Arrays: Understand how to convert different data structures.
  • Finding Common Values: Compare two arrays to find common values.
  • Calculating GCD: Create a function to calculate the greatest common divisor (GCD) of two numbers.
  • Palindrome Checker: Write a function that checks if a given string is a palindrome.

๐Ÿ” Week 4: Advanced Functions and String Manipulation

This week emphasizes sorting, handling duplicate elements, and manipulating strings effectively.

  • Checking Sorted Lists: Write a function to check if a list is sorted.
  • Handling Duplicates: Create functions to detect and remove duplicates from lists.
  • Dictionary Inversion: Write a program to swap keys and values in a dictionary.
  • String Manipulation: Add commas between characters, remove words from strings, and convert sentences to title case without using built-in functions.
  • Binary String Generation: Use recursion to generate all binary strings of a specified length.

๐Ÿ” Week 5: Working with Matrices and Modules

Learn to work with matrices and create custom modules for various applications.

  • Matrix Operations: Define, print, add, and multiply square matrices using Python.
  • Creating Modules: Build modules using geometrical shapes and their operations.
  • Exception Handling: Implement exception handling for robust error management.

๐Ÿ” Week 6: Object-Oriented Programming and Validation

This week focuses on using classes, inheritance, and validation techniques.

  • Drawing Shapes with Classes:
    • Create classes for rectangles, points, and circles, and draw them on a canvas.
    • Add colors and attributes to these shapes to enhance visualization.
  • Method Resolution Order (MRO): Demonstrate MRO in multiple inheritance scenarios.
  • Data Validation: Write programs to validate phone numbers and email addresses.

๐Ÿ” Week 7: File Handling and Text Processing

Master file handling techniques and analyzing text data.

  • File Merging: Combine the contents of two files into a new file.
  • Word Search: Create a function to find specific words in a file.
  • Word Frequency Analysis: Identify the most frequent words in a text file.
  • Text Statistics: Count words, vowels, blank spaces, lowercase, and uppercase letters in a file.

๐Ÿ” Week 8: Python Libraries and GUI Programming

Explore advanced Python libraries and build simple graphical user interfaces.

  • NumPy, Plotly, and Scipy: Learn how to install and use these powerful libraries for data analysis and visualization.
  • Digital Logic Gates: Implement logic gate operations such as AND, OR, NOT, and EX-OR.
  • Adder Circuits: Create programs for Half Adders, Full Adders, and Parallel Adders.
  • GUI Programming: Build a simple window wizard with text labels, input fields, and buttons using Python’s GUI tools.

๐ŸŒŸ Conclusion:

The Python Programming Lab provides you with a strong foundation in Python programming through a wide range of exercises, from basic concepts to advanced topics. By the end of this lab, you will have gained valuable skills in data processing, file handling, OOP, modules, and GUI development. Start coding and enhance your Python skills! ๐Ÿš€


;