I'm taking a course to brush up on my Python skills. (Which aren't many, but so far what I've seen is fairly basic/review. I suspect by the end of the week we'll be learning things that are new to me?) You don't need to know a lot about programming to follow this: (normal is input, bold is output)
x = 10
(this tells the computer what x is. x is now 10)
x + 2
12
(the computer added 2 to x, which is 10, and got 12).
x = 5
(now I give the computer a new value for x. it forgets about the 10 and goes with 5 instead)
x + 2
7
(get it?)
Anyway we're supposed to be learning/reviewing what is a "list" versus a "tuple" etc. right now. But this instructor likes to get sidetracked on how the "memory" works in Python, "stacks" versus "heaps" etc. Again, don't worry too much about what all this means.
x = (1,2)
(I told the computer I want x to be a "tuple," like an ordered pair on a graph.)
y = [0, (5,6), x]
(I made a list called y, whose third element is the tuple called x.)
x = 5
(the same way we did above, we forget that x used to be a tuple, now it's the integer 5. tuples and integers are different "types," but that's okay, x can be anything.)
y[-1]
(asking the computer to output the last element in the list y. course pauses, thinks about what this should be.)
The answer is...
(1,2)
because at the time we defined y, the last element was x, and at that time, x represented (1,2). Even though we've changed x since then, we haven't done anything to y. So as far as the computer knows, the last element of y is (1,2).
Now, this kind of abstract thinking is necessary not just in Python, but I would assume pretty much any programming language, to be able to keep track of what your variables represent and why. And, because I'm an abstract-thinking person and have experience with Python, this is pretty straightforward for me.
But this also seems to be a more 21st-century form of the Sally-Anne test! (Which...doesn't seem to be that reliable a metric anyway, perhaps this is a case in point.)
x = 10
(this tells the computer what x is. x is now 10)
x + 2
12
(the computer added 2 to x, which is 10, and got 12).
x = 5
(now I give the computer a new value for x. it forgets about the 10 and goes with 5 instead)
x + 2
7
(get it?)
Anyway we're supposed to be learning/reviewing what is a "list" versus a "tuple" etc. right now. But this instructor likes to get sidetracked on how the "memory" works in Python, "stacks" versus "heaps" etc. Again, don't worry too much about what all this means.
x = (1,2)
(I told the computer I want x to be a "tuple," like an ordered pair on a graph.)
y = [0, (5,6), x]
(I made a list called y, whose third element is the tuple called x.)
x = 5
(the same way we did above, we forget that x used to be a tuple, now it's the integer 5. tuples and integers are different "types," but that's okay, x can be anything.)
y[-1]
(asking the computer to output the last element in the list y. course pauses, thinks about what this should be.)
The answer is...
(1,2)
because at the time we defined y, the last element was x, and at that time, x represented (1,2). Even though we've changed x since then, we haven't done anything to y. So as far as the computer knows, the last element of y is (1,2).
Now, this kind of abstract thinking is necessary not just in Python, but I would assume pretty much any programming language, to be able to keep track of what your variables represent and why. And, because I'm an abstract-thinking person and have experience with Python, this is pretty straightforward for me.
But this also seems to be a more 21st-century form of the Sally-Anne test! (Which...doesn't seem to be that reliable a metric anyway, perhaps this is a case in point.)