Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does for example 'elements[x]' do?

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading