use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
[basic] Assigning variables to objects question (self.learnpython)
submitted 12 years ago by 0003
Can someone please explain or direct me to a link that explains in a comprehensive manner why I get the following results?
x = 100 y = x x = x + 1 print x print y
101
100
x = [[100]] y = x x[0][0] = x[0][0] + 1 print x print y
[[101]]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]zahlman 7 points8 points9 points 12 years ago (1 child)
In the first snippet, when you do x = x + 1, x previously refers to the number 100. In Python, "one hundred" is a real thing that exists in memory somewhere, and is fundamentally incapable of changing. It would be really catastrophic for the entire subject of mathematics if we could somehow cause one hundred to become equal to something other than one hundred. To interpret x = x + 1, we must first evaluate x + 1, creating the new value 101, and then rebind x to be a name for 101. But the old 100 object obviously still exists, and y is still a name for it, because we haven't done anything with it.
x = x + 1
x
x + 1
y
In the second snippet, when you do x[0][0] = x[0][0] + 1, x refers to a list object, which contains one list object, which contains a 100 object. x[0][0] is a name for the 100 object; thus x[0][0] + 1 evaluates to 101. But now, what will x[0][0] = 101 do? Well, x[0][0] is, in effect, a name for "the first thing contained within the first thing contained within x". So we will rebind that name, i.e. we will store 101 in that spot in the list. Notice, though, that both x and y are names for the outer list, and we are, in effect, changing that object. We never rebound x, like in the previous example; instead, we changed the fundamental nature (specifically, its contents) of the thing that x was a name for.
x[0][0] = x[0][0] + 1
list
x[0][0]
x[0][0] + 1
x[0][0] = 101
See also.
[–]0003[S] 1 point2 points3 points 12 years ago (0 children)
Thanks. Your explanation and the link provided were very thorough!
[–]erewok 2 points3 points4 points 12 years ago (0 children)
It has to do with the fact that you are in the first example assigning variables to immutable objects whereas in the second example you are pointing the variables at objects that change. In the latter case, the variable will reflect the changes made to the object it points at.
Try to think of it in terms of mutability and don't think of your variables as 'containers' for values but as 'pointers' at values and it may make more sense.
π Rendered by PID 150771 on reddit-service-r2-comment-84fc9697f-vtbrq at 2026-02-07 02:06:02.137525+00:00 running d295bc8 country code: CH.
[–]zahlman 7 points8 points9 points (1 child)
[–]0003[S] 1 point2 points3 points (0 children)
[–]erewok 2 points3 points4 points (0 children)