Oct-26-2018, 07:20 PM
(This post was last modified: Oct-26-2018, 07:20 PM by gruntfutuk.)
In Python, variable names point to objects somewhere in memory. Objects are things like integers, strings, lists, and so on. So,
Similarly,
When Python sees an ASSIGNMENT statement i.e.
So, the line
A visualiser helps: http://www.pythontutor.com/visualize.html - post the code in, and step through.
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