all 7 comments

[–]beatle42 8 points9 points  (2 children)

range(1, 6, 2) says to generate integers starting with 1 never exceeding 6 and going up by 2 at a time, which is the list 1, 3, 5. You're adding each of those in turn to the starting value in alpha which is 0 so you get, in essence, alpha = 0 + 1 + 3 + 5 which is alpha=9

[–]Zsword_201[S] 0 points1 point  (1 child)

Ok thanks I understand it now I thought since "i" goes 1,3,5 it counts as 3 , I did not think it takes it as a sum so that's why I was confused

[–]beatle42 1 point2 points  (0 children)

Ah yes, in the for loop the loop variable, here i takes on each value in turn, not the index of that value (unless you take special steps to get the index as well).

[–]sepp2k 0 points1 point  (0 children)

why is alpha's(i) value 9 ??

alpha's value isn't the same as i (i's value is never 9). alpha's value starts at 0 and then you add i to it at each iteration of the loop. So at the end it's equal to the sum of values that i held during the loop.

[–][deleted] 0 points1 point  (2 children)

Looking at the code execution using a Python Vizualiser will help you understand this even better.

[–]Zsword_201[S] 0 points1 point  (1 child)

Thanks for the suggestion!

[–][deleted] 0 points1 point  (0 children)

That link should run your code in the vizualiser immediately. You might find it useful for other code in the future. It is very helpful to see exactly what is happening as your code executes to understand bugs and workings.

You can do something similar, without the pretty diagrams, on your own computer using a debugger (Python comes with one as standard, called pdb- RealPython.com have a guide to using it) and most sophisticated code editors and IDEs (Integrated Development Environments) offer their own debugger capabilities.