+ 2
Is it the correct code for the following assignment ?
# I was trying to solve an assignment with Set in python. I was given a demo set and asked to write a single program that will calculate the sum of all numbers, the number of datum, and the average for the entire dataset. # here is my solution ``` numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9 ,10} def dataset(): total = 0 for item in numbers: total += item print("Average of dataset", total / len(numbers)) print("Number of datum", len(numbers)) return "Sum of all numbers", total print(dataset()) ``` # Output ('Average of dataset', 5.5) ('Number of datum', 10) ('Sum of all numbers', 55)
4 Answers
+ 6
It works.
You can just use sum() in place of the for loop.
Here's a simple one:
numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9 ,10}
total = sum(numbers)
n = len(numbers)
print("Average of dataset", total / n)
print("Number of datum", n)
print("Sum of all numbers", total)
+ 4
numbers = set(range(1, 11)) 😜
+ 2
Thank you
+ 1
Thanks, David