Python Forum
beginning python help request (for loop)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
beginning python help request (for loop)
#1
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.
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#4
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginning Payroll code help EmRandall 6 8,945 Sep-18-2018, 01:21 AM
Last Post: EmRandall
  class and objects beginning python 3 pangea 2 3,067 Dec-15-2017, 12:35 PM
Last Post: mpd

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020