Calculate the sum of all int array elements in Python

Description :

The provided Python script defines a function named ‘sumOfNumbers’ that calculates the sum of all numeric elements within a given list or tuple. It ensures the validity of the input data by checking if it’s a list or tuple and whether the elements are integers or floats. If the input is valid, it computes the sum and returns it. If the input is invalid (e.g., non-numeric elements or non-list/tuple input), appropriate error messages are returned. The script also includes examples demonstrating the usage of the’ sumOfNumbers’ function with different types of input arrays.

content:

 

The content of the provided Python script includes:

1. *Function Definition*:
– The script defines a function named sumOfNumbers(array) that takes one parameter array, representing a list or tuple.
– It checks if the input array is a list or tuple using isinstance.
– It initializes a variable res to zero to store the sum of numeric elements.

2. *Sum Calculation*:
– The function iterates over each element in the array.
– For each element, it checks if it’s an integer or float using isinstance.
– If the element is numeric, it adds it to the running sum stored in res.

3. *Result Return*:
– If the sum res is non-zero, it returns the sum.
– If no valid numeric elements are found in the array, it returns the message “Provide valid data”.

4. *Error Handling*:
– If the input is neither a list nor a tuple, it returns the message “Provide either list or tuple with integers”.

5. *Example Usage*:
– The script provides examples demonstrating the usage of the sumOfNumbers function with different types of input arrays.
– It includes tests with valid lists, valid tuples, invalid lists with non-numeric elements, and invalid input types.

This script is useful for calculating the sum of numeric elements in a list or tuple while ensuring data integrity and providing informative error messages for invalid input.

 

Explanation:

 

The provided code defines a function named sumOfNumbers which calculates the sum of all numeric elements within a given list or tuple.

Here’s a breakdown of the code:

 *Function Definition*:
– The function sumOfNumbers(array) takes one parameter array, representing either a list or a tuple.

 *Type Checking*:
– The function checks if the input array is either a list or a tuple using the isinstance function.

*Sum Calculation*:
– It initializes a variable res to zero to store the sum of numeric elements.
– It iterates over each element in the array and checks if it’s an integer or a float using isinstance.
– If the element is numeric, it adds it to the running sum stored in res.

*Result Return*:
– If the sum res is non-zero, it returns the sum.
– If no valid numeric elements are found in the array, it returns the message “Provide valid data”.

 *Error Handling*:
– If the input is neither a list nor a tuple, it returns the message “Provide either list or tuple with integers”.

 *Example Usage*:
– It demonstrates examples of using the sumOfNumbers function with different types of input arrays.

 

Code:

def sumOfNumbers(array):
    if isinstance(array, list) or isinstance(array, tuple):
        res = 0
        for i in array:
            if isinstance(i, (int, float)):
                res += i
        return res if res else "Provide valid data"
    else:
        return "Provide either list or tuple with integers"

tuple_array = (1, 2, 3, 5)
print(sumOfNumbers([1, 5, 3]))
print(sumOfNumbers(tuple_array))
print(sumOfNumbers(["s"]))
print(sumOfNumbers("string"))

Output:

9
11
Provide valid data
Provide either list or tuple with integers
=== Code Execution Successful ===

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top