0% found this document useful (0 votes)
30 views6 pages

Practical Python Programming Tasks

The document provides examples of Python programs and code snippets to solve problems related to circles, collinear points, merging text files, creating and searching CSV files, printing patterns, calculating Fibonacci series recursively, and calculating the number of months needed to save for a down payment on a house. It includes the source code and output for each problem and asks the user to input variables like annual salary when calculating savings.

Uploaded by

Ayush Sheth
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)
30 views6 pages

Practical Python Programming Tasks

The document provides examples of Python programs and code snippets to solve problems related to circles, collinear points, merging text files, creating and searching CSV files, printing patterns, calculating Fibonacci series recursively, and calculating the number of months needed to save for a down payment on a house. It includes the source code and output for each problem and asks the user to input variables like annual salary when calculating savings.

Uploaded by

Ayush Sheth
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

PRACTICAL-3

COMPILER USED : IDLE SHELL 3.10.2

1. Given the coordinates (x,y) of center of a circle and its radius, determine whether a point lies inside the
circle, on the circle or outside the circle. (Hint: Use sqrt( ), pow(
))

Source code:-

Output:

2. Given three points (x1,y1), (x2,y2) and (x3,y3), check if all the three points fall on one straight line.

Source code:-
Output:

3. WAP to read two text files and merge their contents into a third file.

Source code:-

Output:
4. Write a program in Python to create a CSV file by entering user-id and password, read and search the
password for given user-id.

Source code:-

Output:
5. WAP to ask the number of lines from the user and print the following pattern: 1
AB
234
ABCD

Source code:-
Output:

6. Display the user specified term in Fibonacci series using a recursive function fibo().

Source code:-

Output:

7. You have graduated from PDEU and now have a great job! You move to Banglore and decide that you want
to start saving to buy a house. As housing prices are very high in Banglore, you realize you are going to have
to save for several years before you can afford to make the down payment on a house. We are going to
determine how long it will take you to save enough money to make the down payment given the following
assumptions:

a. Call the cost of your dream home total_cost.

b. Call the portion of the cost needed for a down payment portion_down_payment. For simplicity, assume
that portion_down_payment = 0.25 (25%).

c. Call the amount that you have saved thus far current_savings. You start with a current savings of ₹ 0.
d. Assume that you invest your current savings wisely, with an annual return of r (in other words, at the
end of each month, you receive an additional current_savings*r/12 funds to put into your savings – the
12 is because r is an annual rate). Assume that your investments earn a return of r = 0.04 (4%).

e. Assume your annual salary is annual_salary.


[Link] you are going to dedicate a certain amount of your salary each month to saving for the down
payment. Cal l that portion_saved. This variable should be in decimal form (i.e. 0.1 for 10%).

f. At the end of each month, your savings will be increased by the return on your investment, plus a
percentage of your monthly salary (annual salary / 12).

Write a program to calculate how many months it will take you to save up enough money for a down
payment. You will want your main variables to be floats, so you should cast user inputs to floats.

def calculate_months_to_save():

annual_salary = float(input("Enter your annual salary: "))

Output:

Common questions

Powered by AI

A specific alternating pattern of numbers and letters is printed: odd-numbered rows contain consecutive numbers while even-numbered rows contain consecutive uppercase letters starting from 'A'. This is achieved by using loops and conditions to append either numbers or letters to a line variable based on whether the line number is odd or even .

To determine if a point is inside, on, or outside a circle, calculate the distance from the center of the circle to the point using the formula \(z = (x - x_0)^2 + (y - y_0)^2\), where \((x_0, y_0)\) are the coordinates of the circle's center, and \(r\) is the radius. If \(z = r^2\), the point is on the circle; if \(z < r^2\), the point is inside the circle; and if \(z > r^2\), the point is outside the circle .

To check if three points are collinear, calculate the slope between the first and second points and compare it with the slope between the second and third points. The formula for the slope between two points \((x_1, y_1)\) and \((x_2, y_2)\) is \((y_2 - y_1) / (x_2 - x_1)\). If the slopes are equal, the points are on a straight line .

Create a CSV file with 'User Id' and 'Password' columns using the `csv` module. First, write user-ids and passwords to the file. To retrieve a password, read the CSV file, skip the header, and loop through the rows to find a row with the matching user-id, then return the associated password .

Use a loop to incrementally add to current savings at the end of each month, factoring in monthly income contributions and investment returns. Loop until current savings reach the required down payment amount, tracking the number of months taken. Use variables such as annual salary, saving portion, total house cost, and a fixed return rate to compute the monthly savings increment .

To merge two text files into a third file, open the first two files in read mode and the third file in write mode. Read the contents of the first two files and write them sequentially into the third file. Finally, close all the files to ensure the changes are saved .

In pattern printing, user input directly determines the number of lines patterned, affecting iteration counts and logic branches for output type (number or letter). In file handling, user decisions to continue or terminate an operation affect data persistence, modification, or manipulation processes, altering the program flow significantly based on input commands .

Use a recursive function where the base case returns 1 for the first and second terms. For other terms, the function calls itself with the two preceding terms as arguments. Thus, the nth term is calculated as the sum of the (n-1)th and (n-2)th terms .

Checking if points are collinear involves mathematical calculations of slopes to establish a line or plane property (geometric approach), while merging text files is a task of file I/O operations, dealing with reading from and writing to files (file handling approach). Each employs different logic and operations suitable for their domains .

Use the `pow()` function to calculate the square of the differences between the point and circle center coordinates. Use `sqrt()` to calculate the actual distance when needed, but in this context, comparing squared values is sufficient to determine the point's relative position to a circle without needing square roots .

You might also like