0% found this document useful (0 votes)
3 views17 pages

Assignment 3

Jdjdjkdk

Uploaded by

Who Knows
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)
3 views17 pages

Assignment 3

Jdjdjkdk

Uploaded by

Who Knows
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
You are on page 1/ 17

Deshbandhu College , University of Delhi

Name : Tannu kumari


Course : B.A PROGRAME ( Economics + Hindi ) Major in Economics &
Mino in Hindi
Roll No : 24/0134
1. Write a program to compute the distance between two points, taking input
from the user (Pythagorean Theorem).

Code:

import math

x1, y1 = map(float, input("Enter coordinates of first point (x1, y1): “).split())

x2, y2 = map(float, input("Enter coordinates of second point (x2, y2)“).split())

distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)

print(f"The distance between the points is: {distance}")


Output :
2. Write a program using a for loop that loops over a sequence.
What is the sequence?
Code:
sequence = [1, 2, 3, 4, 5]
for num in sequence:
print(num)
Output :
3. Write a program using a while loop that asks the user for a number and
prints a countdown from that number to zero.
Code :
n = int(input("Enter a number for countdown: "))
while n >= 0:
print(n)
n -= 1
Output :
4. Find the sum of all Fibonacci numbers below two million. Each new term in the Fibonacci
sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10
terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, . . .
Code:

a, b = 1, 2
sum_fib = 0
while a < 2000000:
sum_fib += a
a, b = b, a + b
print(f"Sum of all Fibonacci numbers below two million: {sum_fib}")
Output :
5. By considering the terms in the Fibonacci sequence whose values do not exceed four
million, find the sum of the even-valued terms.

Code:
a, b = 1, 2
sum_even_fib = 0
while a < 4000000:
if a % 2 == 0:
sum_even_fib += a
a, b = b, a + b
print(f"Sum of even Fibonacci numbers below four million:
{sum_even_fib}")
Output :
Output :
7. Find the mean, median, and mode for a given set of numbers in a list. You can
also write a function to do the same.
Code:
import statistics
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
mean = statistics.mean(numbers)
median = statistics.median(numbers)
try:
mode = statistics.mode(numbers)
except statistics.StatisticsError:
mode = "No unique mode"
print(f"Mean: {mean}")
print(f"Median: {median}”)
print(f"Mode: {mode}")
Output :
8. Write a function dups to find all duplicates in a list.
Code:
def dups(lst):
duplicates = [ ]
seen = set( )
for item in lst:
if item in seen and item not in duplicates:
duplicates.append(item)
else:
seen.add(item)
return duplicates
lst = [1, 2, 3, 2, 4, 5, 1]
print(f"Duplicates: {dups(lst)}")
Output :

You might also like