Array Summation Function - Python Programming Exercise

In this exercise, you will develop a Python program to calculate the sum of the elements in an array. The main function should look like this: def main(): example = [20, 10, 5, 2] print("The sum of the example array is {}".format(sum_elements(example))). You must define the function sum_elements, and it will be called from within the main function. As shown in the example, it must accept an array as a parameter and return the sum of its elements. This exercise is perfect for practicing function definition and calling in Python. By implementing this function and calling it from the main function, you will gain hands-on experience in handling function definitions and calls in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Array Summation Function

 Objective

Develop a Python program to calculate the sum of the elements in an array. The main function should look like this:

def main():
example = [20, 10, 5, 2]
print("The sum of the example array is {}".format(sum_elements(example)))

You must define the function `sum_elements`, and it will be called from within the main function. As shown in the example, it must accept an array as a parameter and return the sum of its elements.

 Example Python Exercise

 Copy Python Code
# Define the function sum_elements which calculates the sum of the elements in the array
def sum_elements(arr):
    total = 0  # Initialize the total sum to 0
    for num in arr:  # Loop through each number in the array
        total += num  # Add the number to the total sum
    return total  # Return the total sum

# Main function to call sum_elements and display the result
def main():
    example = [20, 10, 5, 2]  # Define the example array
    # Print the sum of the elements in the example array
    print("The sum of the example array is {}".format(sum_elements(example)))  # Call the function and format the result

# Call the main function to execute the program
main()

 Output

The sum of the example array is 37

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Double Value Function

    In this exercise, you will develop a Python function named "double" to calculate and return an integer that is twice its input. This exercise is perfect for pr...

  •  Function Double with Mutable Parameter

    In this exercise, you will develop a Python function named "double_value" to calculate the double of an integer number and modify the data passed as an argument. Sinc...

  •  Function Swap with Mutable Parameters

    In this exercise, you will develop a Python program with a function called "swap" to exchange the values of two integer variables, which are passed by reference. This...

  •  Optimizing Local Variables in Functions

    In this exercise, you will develop a Python program with a function named "power" to compute the result of raising one integer to the power of another positive intege...

  •  Recursive Function for Calculating Powe

    In this exercise, you will develop a Python program with a function that computes the result of raising one integer to the power of another integer (e.g., 5 raised to...

  •  Calculating Fibonacci Sequence with a Function

    In this exercise, you will develop a Python program that uses a recursive function to determine a number in the Fibonacci sequence. In this series, the first two numb...