Write a Python Program to find sum of array

Write a Python Program to find sum of array.

Write a Python Program to find sum of array.

In Python, an array is a data structure used to store a collection of elements, each identified by an index or a key. Unlike some other programming languages, Python does not have a built-in array type. Instead, the most commonly used array-like data structure is the list.
A list in Python is a dynamic array, meaning it can change in size during runtime. Elements in a list can be of different data types, and you can perform various operations such as adding, removing, or modifying elements. Lists are defined using square brackets [] and can be indexed and sliced to access specific elements or sublists.

Example of a simple list in Python:
my_list=[1,2,3,4,5]

Program
def myfun():
    arr=[1,2,3,4,5]
    ans=sum(arr)
    print("Sum of the array is",ans)
    
if __name__=="__main__":
    myfun()


Output:

Sum of the array is 15


Another way
def sum_of_array():
    arr=[1,2,3,4,5]
    total=0
    for element in arr:
        total=total+element
    print("Sum of the array is",total)
    
if __name__=="__main__":
    sum_of_array()


Output:

Sum of the array is 15



More Questions


83 . Write a Python Program for cube sum of first n natural numbers?
84 . Write a Python Program to find sum of array.



For latest job updates join Telegram Channel: https://t.me/sateeshm