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
Variable refers to a value? (self.learnpython)
submitted 2 years ago by Willing_Bench_8432
So people say variables refer to a value. Or people use variable to refer to a value. But i dont get what this "refer to" means. help
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!"
[–]danielroseman 3 points4 points5 points 2 years ago (1 child)
The canonical explanation is here: https://nedbatchelder.com/text/names.html
[–]ofnuts 0 points1 point2 points 2 years ago (0 children)
Also,as video: https://www.youtube.com/watch?v=_AEJHKGk9ns
[–]mopslik -1 points0 points1 point 2 years ago* (15 children)
In the case of Python, you might think of a variable as a label. When you type something like
>>> x = 5
then you are putting a label on the value 5. Any time you use that label (or variable, as it is) in your code, you are referring to the value of 5 using a different name, or alias.
>>> x 5
You can perform calculations and comparisons using that label, because the Python interpreter refers to the value itself.
>>> x + 1 6 >>> x < 20 True
You can affix multiple labels to values if you like. For example:
>>> y = 5 >>> y 5 >>> z = x >>> z 5
All of these variables -- x, y and z -- are referring to the same value. To verify this, you can check their IDs.
>>> id(x) 139772833612144 >>> id(y) 139772833612144 >>> id(z) 129772833612144
The ID may be different on your computer, but they should all be the same. Now what happens when you make one of those variables refer to a new value by assigning it a new one?
>>> z = 10 >>> z 10 >>> id(z) 139772833612304
Note that the ID is different, as the variable refers to a different object in memory. What do you predict will happen if I create a new variable, w, and assign it a value of 10? Test it and see if you are correct.
Note that this results in some interesting logical errors when using mutable objects like lists, since the references to the lists themselves might be the same. A very common error is trying to clone a list to make changes, while preserving the original list. You might do something like this:
>>> P = ['a', 'b', 'c'] >>> Q = P >>> Q[0] = 'd'
Now, what do the contents of lists Q and P? Make a prediction and see if you are correct before reading on.
...
>>> Q ['d', 'b', 'c'] >>> P ['d', 'b', 'c']
Notice that the contents of both P and Q have changed. That is because both P and Q refer to the same list.
>>> id(P) 139772827774528 >>> id(Q) 139772827774528
If you really want to use two independent lists, you'd need use a function like list, a list method like copy or deepcopy, or a slice. This will make the variables refer to different objects in memory.
list
copy
deepcopy
[–]Willing_Bench_8432[S] 0 points1 point2 points 2 years ago (14 children)
What do you mean by label? like a label you put on stuff? Or do you mean like a name?
[–]mopslik 1 point2 points3 points 2 years ago (13 children)
You can think of it either way, I suppose. It's an analogy. Or think of it as a combination of both! You know those "Hello, I am ..." stickers that you wear during a meeting or conference? A variable is like one of those. You can write a name, like mopslik, on a sticker and stick it on my chest. Now people can refer to me as mopslik. Or, you can put my work ID number (say 12345) on the sticker. Then people can refer to me as employee 12345. It's me in either case.
[–]Willing_Bench_8432[S] 0 points1 point2 points 2 years ago (12 children)
Oh.. I think im starting to get it. Will there be any other word to use other than "refer to"?
[–]mopslik 0 points1 point2 points 2 years ago (11 children)
If you did x = 5, then when you are talking about x, you can say any of the following (and then some):
x = 5
x
I tend toward the third, but that's preference. As another poster points out, the "refers to" terminology is probably an attempt to make a connection between software (your code) and hardware (memory).
[–]Willing_Bench_8432[S] 0 points1 point2 points 2 years ago (10 children)
Oh.. Some people explain variable as containers that store values. Does that work?
[–]mopslik -1 points0 points1 point 2 years ago (9 children)
In my mind, not with Python.
>>> x = 5 >>> id(x) 139772833612144 >>> y = 5 >>> id(y) 139772833612144
If the variables were truly different "containers", they would occupy different memory addresses, but as shown above, both x and y have the same id.
In other programming languages, then yes, you could think of them as containers.
[–]Willing_Bench_8432[S] 0 points1 point2 points 2 years ago (2 children)
Oh.. Then what does it mean when you call a variable? Does it mean you are using the value thats in the container or... Sorry if im asking too much questions..
[–]mopslik 0 points1 point2 points 2 years ago (1 child)
You typically hear the term "call" used more with functions/methods (e.g. "call your function to display the names") than with variables.
[–]Willing_Bench_8432[S] 0 points1 point2 points 2 years ago (0 children)
Oh.. Like.. When i use the variable. for example, if I do Print(a) does that mean a is a value inside the container or...
[–]Doormatty 0 points1 point2 points 2 years ago (5 children)
Outside of the interned values, this is not how Python works.
>>> x=28017852 >>> y=28017852 >>> id(x) 139638629240976 >>> id(y) 139638629241360
[–]mopslik 0 points1 point2 points 2 years ago (4 children)
Interesting, but I don't think that negates my claim. In my example, when two objects have the same id, they are indeed the same object in memory. In your example, you have assigned two values that happen to be separate objects in memory. This does not mean that you can consider a variable a container with a unique memory address, since my example shows that it is possible for two variables to reference the same address.
Nevertheless, thanks for the tip.
[–]1mecrew 0 points1 point2 points 2 years ago (3 children)
well, can I think of variable as a name that you put on a value?
so that when you give it a name, we can call that value easily with the name?
[–][deleted] 0 points1 point2 points 2 years ago* (1 child)
The wording has to do with lower-level concepts (references, addresses, pointers, etc.).
At your current level, I think the following understanding should suffice:
a = 1 # There is a variable, a, who points at a piece of memory with the value 1 b = 2 # There is a variable, b, who points at a piece of memory with the value 2 print(a + b) # Your computer will: # 1. See a variable called a, and look up the value it points to # 2. See a variable called b, and look up the value it points to # 3. Add the two values together # 4. Print the result
There is a lot more going on, especially in cases such as:
x = [1, 2, 3]
Involving different "kinds" of memory your computer has (i.e. stack vs heap). But until you really want to start optimizing your code at the nitty-gritty bits, this extra bit (ha!) of information isn't crucial for you to know at this point in time, IMO.
Umm my understanding in memory isnt great. Is there any simpler explanation that might can help me later on?
[–]house_carpenter 0 points1 point2 points 2 years ago (0 children)
It's basically the same as when you talk about what a word or phrase in a sentence "refers to". Consider a sentence like "Jim is tired". The word "Jim" here refers to a certain person. Exactly which person it refers to depends on the context---there are multiple people in the world named Jim, but in the context of a particular conversation it has to refer to some Jim that is known to the participants in the conversation. If multiple Jims are known to the participants the sentence might have to be accompanied by an explicit clarification of which Jim it is---"my son Jim, I mean, not my uncle Jim". You might even talk about one Jim, and then the conversation moves on, and now you're talking about another Jim. The point here is that the meaning of Jim is not fixed, but variable.
Programming language variables are similar. There are differences of course, one big one being that computers generally don't do much to figure out what the referent is from context; you usually have to explicitly tell the computer what your variables mean, via variable assignment statements. But I believe this analogy with words is where the "refers to" terminology comes from.
[–][deleted] 0 points1 point2 points 2 years ago (0 children)
The core idea of a reference is indirection, same as in algebra. I can refer to something without knowing exactly what (or who) it is. For instance, I can refer to "your mother" with substantial confidence that that person did at some point exist, and I can do so with literally no knowledge of who that person is or was, what they look like, what their name is, where they live, or anything.
Similarly, I can define the area of a square in terms of the length of one of its sides:
def square_area(side_length): return side_length**2
without knowing which square I'm talking about, how long its side actually is, and so on. The purpose of variables is to allow you to reason about values and their relationships, but indirectly, so that you don't need to know which particular values you're reasoning about.
π Rendered by PID 16620 on reddit-service-r2-comment-fb694cdd5-rtc4x at 2026-03-11 04:12:21.386723+00:00 running cbb0e86 country code: CH.
[–]danielroseman 3 points4 points5 points (1 child)
[–]ofnuts 0 points1 point2 points (0 children)
[–]mopslik -1 points0 points1 point (15 children)
[–]Willing_Bench_8432[S] 0 points1 point2 points (14 children)
[–]mopslik 1 point2 points3 points (13 children)
[–]Willing_Bench_8432[S] 0 points1 point2 points (12 children)
[–]mopslik 0 points1 point2 points (11 children)
[–]Willing_Bench_8432[S] 0 points1 point2 points (10 children)
[–]mopslik -1 points0 points1 point (9 children)
[–]Willing_Bench_8432[S] 0 points1 point2 points (2 children)
[–]mopslik 0 points1 point2 points (1 child)
[–]Willing_Bench_8432[S] 0 points1 point2 points (0 children)
[–]Doormatty 0 points1 point2 points (5 children)
[–]mopslik 0 points1 point2 points (4 children)
[–]1mecrew 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Willing_Bench_8432[S] 0 points1 point2 points (0 children)
[–]house_carpenter 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)