Python Forum
beginning python help request (for loop) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: beginning python help request (for loop) (/thread-13680.html)



beginning python help request (for loop) - casey - Oct-26-2018

Learning 'for' loops and having trouble wrapping my head around this one:

result = 1
for indx in range (10):  
    if (indx % 2 == 0): continue  
    result = result * indx 
I think I understand what lines 1-3 do.
  • Line 1 assigns an integer to result object.
  • Line 2 begins the for loop, calling upon the range function to generate a integer range 0-9.
  • Line 3 is an if statement that says if the remainder of the indx divided by 2 is equal to 0, then skip the indx and continue to next value. This effectively skips all even integers.

I cannot figure out what line 4 is doing (what does "result * indx" actually do?). I know I'm missing something fundamental but my web searching hasn't yielded the information I need.

the value of result after running the program is 945.

Thanks in advance if you can point me in the right direction.

**I'm in a non-credit python programming class. the course is not graded and the instructor does not mind us getting help.


RE: beginning python help request (for loop) - buran - Oct-26-2018

on line 4 current value of result is multiplied by current value of indx (that is right-hand side of the equal sign) and the result is assigned as new value of result
maybe if you add two print functions it will help
result = 1
for indx in range (10):  
    if (indx % 2 == 0):
        continue # I moved this on separate line just for more redability
    print('result before multiplication: {}, indx: {}'.format(result, indx))
    result = result * indx
    print('result after multiplication: {}'.format(result))
Note that you may see something like this
result *= indx
This is the same, but written a bit shortly


RE: beginning python help request (for loop) - gruntfutuk - Oct-26-2018

In Python, variable names point to objects somewhere in memory. Objects are things like integers, strings, lists, and so on. So, result = 1 means result refers to the object somewhere in memory that is the computer representation of the object 1.

Similarly, indx refers to an integer object somewhere in memory being used as a counter. Each time you go around the loop, that object has one added to it but indx still refers to it. (Actually, some objects in Python, including low integer numbers, are predefined so indx might just be changed to refer to the next higher object - that's an implementation detail though, it amounts to the same thing for the programmer).

When Python sees an ASSIGNMENT statement i.e. something = then it knows the variable something must be made to refer to what ever object is the outcome of the expression on the right of the =.

So, the line result = result * indx means assign the object that is the outcome of the expression result * indx to the variable result. When evaluating the expression, Python looks at the objects referred to by the variables, does any required calculations, which generates a new object (or, identifies a pre-defined object) and assigns that to the variable on the left of the =, overwriting any previous reference that variable might have held.

A visualiser helps: http://www.pythontutor.com/visualize.html - post the code in, and step through.


RE: beginning python help request (for loop) - casey - Oct-26-2018

THANK YOU!!!!

Your replies helped a ton and I understand now.

I didn't realize that the result object was redefined with a new value every time the loop ran. I thought it remained static at "1". Now it makes total sense!