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

Why is this my output using index? what is index?

for index in range(0, 10, 2):
    print(index)

Why is the output 2,4,6,8?
How is it specified to only print evens? Is this what index does? My understanding was that index means position.

>Solution :

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

As was explained in the comments, the word index is just a variable name here. Yes, normally index refers to the position of an element in an iterable, however, that is not related to anything occurring in the code snippet in question.

Only the evens are printed as a result of the built-in range function. The third parameter, in this case 2, specifies the "step," which basically says how many numbers to jump for each element in the iterable range constructs. Then those elements are iterated over and bound to index in order, thus you see your output.

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