Python: Calculate the sum over a container
Sum of Container Items
Write a Python program to calculate the sum of all items of a container (tuple, list, set, dictionary).
Some objects contain references to other objects; these are called containers. Generally, containers provide a way to access the contained objects and to iterate over them. Examples of containers are lists, sets, tuples and dictionaries.
Pictorial Presentation:
Sample Solution:
Python Code:
# Calculate the sum of the elements in the list [10, 20, 30] using the 'sum' function and store it in the variable 's'
s = sum([10, 20, 30])
# Print a message indicating the sum of the container and display the value of 's'
print("\nSum of the container: ", s)
# Print an empty line for better readability
print()
Sample Output:
Sum of the container: 60
list container:
Python Code:
# Create a list named 'nums' containing three integer elements: 10, 20, and 30
nums = [10, 20, 30]
# Print a message indicating the original container
print("Original container:")
# Print the 'nums' list, displaying its contents
print(nums)
# Print the data type of the 'nums' list
print(type(nums)
# Print a message indicating the sum of all items in the 'nums' list and compute the sum using the 'sum' function
print("Sum of all items of the said container:", sum(nums))
Sample Output:
Original container: [10, 20, 30] <class 'list'> Sum of all items of the said container: 60
dictionary container:
Python Code:
# Define a function 'dict_sum' that takes a dictionary 'nums' as input and calculates the sum of its values.
def dict_sum(nums):
num_sum = 0
for i in nums:
num_sum = num_sum + nums[i]
return num_sum
# Create a dictionary 'nums' with key-value pairs.
nums = {'a': 100, 'b': 200, 'c': 300, 'd': 120}
# Print a message indicating the original container and display the dictionary 'nums'.
print("Original container:")
print(nums)
# Print the type of the 'nums' container.
print(type(nums))
# Calculate the sum of all items in the dictionary 'nums' using the 'dict_sum' function and print the result.
print("Sum of all items of the said container:", dict_sum(nums))
Sample Output:
Original container: {'a': 100, 'b': 200, 'c': 300, 'd': 120} <class 'dict'> Sum of all items of the said container: 720
set container:
Python Code:
# Create a set 'nums' with a collection of numbers.
nums = {7, 4, 9, 1, 3, 2}
# Print a message indicating the original container and display the set 'nums'.
print("The original container")
print(nums)
# Print the type of the 'nums' container.
print(type(nums))
# Calculate the sum of all items in the set 'nums' using the 'sum' function and store it in the 'sum_tuple' variable.
sum_tuple = sum(nums)
# Print the sum of all items in the set.
print("Sum of all items of the said container:", str(sum_tuple))
Sample Output:
The original container {1, 2, 3, 4, 7, 9} <class 'set'> Sum of all items of the said container: 26
tuple container:
Python Code:
# Create a tuple 'nums' with a collection of numbers.
nums = (7, 4, 9, 1, 3, 2)
# Print a message indicating the original container and display the tuple 'nums'.
print("The original container")
print(nums)
# Print the type of the 'nums' container.
print(type(nums))
# Calculate the sum of all items in the tuple 'nums' using the 'sum' function and store it in the 'sum_tuple' variable.
sum_tuple = sum(nums)
# Print the sum of all items in the tuple.
print("Sum of all items of the said container:", str(sum_tuple))
Sample Output:
The original container (7, 4, 9, 1, 3, 2) <class 'tuple'> Sum of all items of the said container: 26
Python Code Editor:
Previous: Write a Python program to concatenate N strings.
Next: Write a Python program to test if a certain number is greater than all numbers of a list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics