Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Sum of Array

Add a fixed list of integers using a loop. Students trace the accumulator's initial value, repeated updates, and a final print after the complete traversal.

BeginnerPhase 4 - Level 1: Basic ArraysExample 2 of 10
sum-of-array.py
1# Array
2arr = [10, 20, 30, 40, 50]
3
4# Calculate sum
5sum = 0
6for element in arr:
7 sum += element
8
9print(f"Sum: {sum}")

Output

Sum: 150

In this one

  • Accumulate list values
  • Initialize a neutral total
  • Separate data from input

How it works

  1. 1

    Start with the provided list and sum zero.

  2. 2

    Add every element to sum.

  3. 3

    Print the total after traversal.

Edge cases

What if the list were empty?

What if the list were empty?

The total would remain zero.

Are repeated values added repeatedly?

Are repeated values added repeatedly?

Yes. Every position contributes.

What's going on

The list is already provided, so no input prompts are needed. sum starts at zero and each element is added once.

The running totals are ten, thirty, sixty, one hundred, and one hundred fifty. The final print is outside the loop, so only the completed total appears. Naming a variable sum shadows the built-in function; it does not call that function.

What if the list were empty? The total would remain zero.

Are repeated values added repeatedly? Yes. Every position contributes.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is the sample total?

Pick an answer to check it.

Question 2

What if the list were empty?

Pick an answer to check it.

Questions people ask

What if the list were empty?

The total would remain zero.

Are repeated values added repeatedly?

Yes. Every position contributes.

What is the sample total?

150.