I was trying to make a for loop to sum the integers from 1 to 100 in Python and wrote the code like this :
for i in range(1, 101) :
sum_list = []
sum_list.append(i)
print(sum(sum_list))
but it didn't print the output I wanted (5050) and the VSCode python interpreter showed the message that output exceeded the size limit.
I want to know what causes this error and what I should do if I want to get the correct output.
You were not getting the result because you were initializing the list inside the for loop. You need to initialize in the beginning before starting for loop.
sum_list = []
for i in range(1, 101):
sum_list.append(i)
print(sum(sum_list))
#5050
2.You can get the result my list comprehension as well.
sum_list=[i for i in range(1,101)]
print(sum(sum_list))
#5050
3.another way:
sum_list=list(range(1,101))
print(sum(sum_list))
#5050
4.Directly sum generator expression:
sum(i for i in range(1,101))
#5050