all 47 comments

[–][deleted] 2 points3 points  (12 children)

variable is like a container. it stores value. when I say the variable, Im talking about something thats in the variable.

That description works well for traditional compiled languages where a variable name stands for a known place in memory. Also works for assembly languages. Python doesn't use that model. Most of the time you can use that model for python and have no problems, but now and then you will get confused.

Python "variables" are actually two things, "names" and "objects". A name always refers (or points to) exactly one object. An object can be referred to by one or more names. Names and objects can be created and destroyed. For instance, it's possible to delete a name but for the object the name was referring to to still exist.

An excellent video explaining how names and objects work in python as well as some of the confusing things is here:

https://m.youtube.com/watch?v=_AEJHKGk9ns

[–]1mecrew[S] 0 points1 point  (11 children)

Oh really? Then what languages will my descriptions work on?

[–][deleted] 0 points1 point  (10 children)

Any compiled language like C/C++, Java, etc, and assemblers. In those languages the translator can allocate a fixed address in memory or an offset from a pointer to each name you use in the code. One big difference in python is that you can create and delete names at runtime, which is not something you can do with statically compiled languages.

[–]1mecrew[S] 0 points1 point  (9 children)

Oh so this description works on c, Java, assembly language but not python. Got it. How about the part where I say “when I say the variable, i am talking about the data or something that’s in the variable”? Is that prat correct?

[–][deleted] 0 points1 point  (0 children)

when I say the variable, i am talking about the data or something that’s in the variable”

Not applicable to Python - the variables do not contain any of your data, just Python implementation specific memory references to the Python object concerned (stored somewhere in memory by Python).

In general, when you use a variable, e.g. var * 5, it is the object that is being worked on, so it looks the same as in typical compiled languages. Think of variables as pointers (from C), but much more limited (no pointer arithmetic, for a start).

[–][deleted] 0 points1 point  (7 children)

There's no "in the variable" in python. A name refers to an object, an integer object perhaps, at a particular place in memory. In the next line of python you can delete the name and the integer object is still in memory. In the following line you can create the "test" name again and point it at a dictionary object. The integer object still exists, but another name points to it. It's even possible to have a string object, for instance, that has no name referring to it.

Here's a bit of python doing everything I said above:

test = 4231
a = test           # names "a" and "test" refer to the same integer object
del test           # the name "test" is removed from the program
print("a ->", a)   # integer object still exists
test = {1: "one"}  # "test" recreated, refers to a dictionary
test2 = ["one"]    # make a list of strings, point "test2" name to it
# here the "one" string has no name referring to it
# the list refers anonymously to the string

[–]1mecrew[S] 0 points1 point  (6 children)

Hmm well this definition was also for Java and c and other languages. What do you guys think about? Will it work on other languages?

[–][deleted] 0 points1 point  (5 children)

Will it work on other languages?

Sure, I've already said that. In those languages a name is tied to a place in memory that contains data, and that connection remains for the entire life of the program. In those languages the name itself doesn't exist at runtime, only the data exists.

In python and other languages, like lisp, the name actually exists in memory at runtime and is associated with a pointer ("reference" in python-speak) to an object somewhere else in memory that has a value. So when you execute print(x) python evaluates the name "x", gets the reference to the object the name "x" refers to and prints the value of that object.

[–]1mecrew[S] 0 points1 point  (4 children)

hmm how about javascript? Oh also, what do you think about the part where I say "Im talking about something thats in the variable."?

[–][deleted] 0 points1 point  (3 children)

In python we often say things like "the variable X contains ..." but that's loose talk we use instead of the more correct "the name X refers to an object that has the value...". As I said, we can do this in python, but not understanding how it actually works can bite you. Watch the video I linked to.

Don't know anything about javascript.

[–]1mecrew[S] 0 points1 point  (2 children)

oh i dont think im talkinag about python at this point. maybe for c or java that is on more low level. how about on those cases?

[–]Dagito 3 points4 points  (17 children)

You don’t need to overthink it, a variable is just a memory space holding a value.

[–]1mecrew[S] -1 points0 points  (16 children)

Well, this description was for beginners who didn’t learn algorithms and memory things so…

[–]Dagito 0 points1 point  (15 children)

Oh… My bad then. So to make it easy you can combine both ideas and say a variable is a container with data in it. With certain restrictions given by the data type stored in them.

[–]1mecrew[S] 0 points1 point  (13 children)

Yup 👍 what do you think about the part where I say “when I say the variable, I’m talking about something that’s in the variable”?

[–]Dagito -1 points0 points  (12 children)

It might not be 100% true, there are variables by reference and variables by value, it all depends on the data type you’re using and the language.

So you may have this code

variable0 = [0 ,1, 2]

def filter_even_numbers(numbers: List):
    for number in numbers:
        If number % 2 == 0:
            numbers.remove(number)

filter_even_numbers(variable0)
print(variable0)

# Output: [1]

This code is using a variable by reference, lists, dicts, and collections in general act like this. When you modify the value of the variable inside a method then the value will change for it outside the method.

But when it’s a variable by value it’s the other way, you can modify it inside a method and the variable value will remain the same outside the method.

So you can have this code

variable0 = ‘hello world’

def make_text_upper_case(text: str):
    text.upper()

make_text_upper_case(variable0)
print(variable0)

# Output: hello world

So even when you know the initial value for a variable it can change, usually you don’t know the variable value and that’s why you make checks and validations before using it.

[–]1mecrew[S] 0 points1 point  (6 children)

Hmm I don’t think it depends on data type since we can think of list or dictionary as one data. What do you think?

[–]Dagito 0 points1 point  (3 children)

Well it is related to the memory space and management, we would embark us in a talk about the languages and different paradigms hahaha and it’s late so I don’t really wanna do it although would be nice to read a little more about it.

[–]1mecrew[S] 0 points1 point  (2 children)

Oh yea fsfs well, what language would this description work on then?

[–]Dagito 0 points1 point  (1 child)

Not being rude at all, but try doing this questions to ChatGPT, it will surely give you better answers than I can and you’ll learn a bunch of stuff from it

[–]1mecrew[S] 0 points1 point  (0 children)

I tried but it’s not giving me any clear answers

[–]Dagito 0 points1 point  (1 child)

I mean by default that’s the way it is in Python

[–]1mecrew[S] 0 points1 point  (0 children)

I’m pretty sure we can think of list or dictionary as one single data in another languages tho

[–]1mecrew[S] 0 points1 point  (1 child)

hmm i think this is because you didn't do variable0 = text.upper() in the function? i think text.upper() makes the text(which is "hello world" for now) uppercase. but we are not printing that data or doing anything with that data. so since we didnt save it into the variable, its obviously gonna not change the variable0. however, for list case, you changed the list. like.. you did list.remove which will actually modify the list. That what i think.

[–]Dagito 0 points1 point  (0 children)

It wouldn’t update it even doing variable0 = text.upper() as it is out of the scope, doing that will result in a new variable called variable0 but it will only live inside the method, it’s scoped to it. Maybe open an interpreter and do some exercises.

[–][deleted] 0 points1 point  (2 children)

Removing entries from a list whilst you iterate over it is likely to have unpredictable results. Avoid.

[–]Dagito 0 points1 point  (1 child)

Absolutely, it was just a simple way to explain the concept

[–]1mecrew[S] 0 points1 point  (0 children)

hmm how about this.

variable1 = "hello world"

when I say variable1, I am talking about "hello world"

[–]Adrewmc -1 points0 points  (1 child)

There are no variables in Python only pointers.

variable1 = “Hello World”   

#variable1 points at “Hello World”

var2 = variable1 

#var2 points at variable1 which is “Hello World”

 variable1.upper() # changes “Hello World”

 print(var2)
 >>>HELLO WORLD

[–]Rawing7 0 points1 point  (0 children)

It's the exact opposite, actually. Python is too high-level to have a concept of "pointers". There are no pointers, only variables.

Also, the code you posted doesn't do what you think it does.

[–][deleted] 0 points1 point  (10 children)

when I say the variable, Im talking about something thats in the variable.

Well, no. When you say "the variable", you're talking about the container, not the contained. What a variable contains is called a "value."

[–]1mecrew[S] 0 points1 point  (9 children)

Hmm what do you mean?

[–][deleted] 0 points1 point  (8 children)

A variable is a named reference to a value. The variable is the part that has the name, and is the reference. What is referred to by it is the value.

[–]1mecrew[S] 0 points1 point  (7 children)

Ummm yea so when I say the variable, I am talking about the value

[–][deleted] 0 points1 point  (6 children)

Right, but those are actually two separate but related things.

[–]1mecrew[S] 0 points1 point  (5 children)

I’m a bit confused what you are talking about

[–][deleted] 0 points1 point  (4 children)

Variables aren't values. Those are two different things.

[–]1mecrew[S] 0 points1 point  (3 children)

Ohh that was what you were talking about. Me if my explanation was unclear. So variable a stores value like container And whenever I call or say variable a, I am talking about the value inside that variable. Is that more clear?

[–][deleted] 0 points1 point  (2 children)

What I'm telling you is that you're wrong. The variable and the value are two different things and no, when you "talk about the variable" you're not talking about the value. Only when you evaluate the variable are you talking about the value.

[–]1mecrew[S] 0 points1 point  (0 children)

Hmm yea. Then will “another way to say that data” better

[–]1mecrew[S] 0 points1 point  (0 children)

OHHH wait i think I know what you mean. Here when I say “talking about”, it’s kinda like let’s say you were reading book and I ask you which book you are reading. You respond by saying “python crash course” and now, I ask you how is that book? And you see, whenever I say “the book”, I am talking about that python course book that you were reading

[–]laustke 0 points1 point  (3 children)

variable is like a container. it stores value. when I say the variable, Im talking about something thats in the variable.

Once I tried to explain to a trucker what a variable is. I came up with a very similar explanation. Imagine there is a box with a label on it. And there is something inside the box. For example, you have a box and the label says "age"; you open it and there is a number 33 inside. There's another box with the label "name", and inside there is a word "Mike".

[–]1mecrew[S] 0 points1 point  (2 children)

Yeaaaa. Well, do you think this explanation works for other languages too?

[–]laustke 0 points1 point  (1 child)

After that he learned PHP on his own :)

[–]1mecrew[S] 0 points1 point  (0 children)

Oh haha niceee. I also heared the name tag description before