So I just started learning python, my first programming language and I found this in CS Dojo’s video and I don’t understand this line ‘given_list[i]‘. So we have a dictionary called ‘given_list[]‘ and a variable called ‘i‘. If I combine this two together then what will happen?
given_list = [5, 4, 4, 3, 2, 10]
total3 = 0
i = 0
while i < len(given_list) and given_list[i] > 0:
total3 += given_list[i]
i += 1
print(total3)
Thank you in advance!
>Solution :
Congrats on your first venture in programming!
First off, given_list is not a dictionary, but rather a list. Think of it as a collection of items in one single variable. The [] is known as the indexing operator, which is the way we get elements from the list at a certain index. In python, and many other programming languages, the first element in the list has index 0. So in this case, the variable i is used as an index to loop over each element in the list and perform the calculations necessary.