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)
#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


Messages In This Thread
beginning python help request (for loop) - by casey - Oct-26-2018, 07:06 PM
RE: beginning python help request (for loop) - by gruntfutuk - Oct-26-2018, 07:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginning Payroll code help EmRandall 6 9,076 Sep-18-2018, 01:21 AM
Last Post: EmRandall
  class and objects beginning python 3 pangea 2 3,164 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