Assignment 3
Assignment 3
Code:
import math
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 :