Computer Science

What will the following snippet print?

L5 = [1500, 1800, 1600, 1200, 1900] 
begin = 1
sum = 0
for c in range(begin, 4):
    sum = sum + L5[c]
    print(c, ':', sum)
    sum = sum + L5[0] * 10
    print("sum is", sum)

Python

Python List Manipulation

2 Likes

Answer

Output
1 : 1800
sum is 16800
2 : 18400
sum is 33400
3 : 34600
sum is 49600

Working

The given code initializes a list L5 with five elements. It then sets begin to 1 and sum to 0. The loop iterates for values 1, 2, and 3. In each iteration, the code adds the element at index c of L5 to sum, prints the c and updated sum, and then adds ten times the first element of L5 to sum and prints the value of sum.

Answered By

2 Likes


Related Questions