Python Variable Assignment and Memory Location

Python Variable Assignment and Memory Location

Open your kitchen cupboard. In it you will find various means of storing food. Cardboard boxes, Ziplocs, Tupperware, plastic bags sealed with twist ties, and the focus of our discussion, storage jars. Computer memory is like a cupboard filled with storage jars. Using Python, you put values in the jars and then you put a label, a variable, on the jar, so you can find your value later.

The storage jar is our location in memory where I want to keep my peanut butter and jelly. Suppose I have a value of peanut butter, 5. We can see the location of the memory address of that value with the id() function.

Think of that long number as a storage jar.

I can put a label on the jar:

I can check the location of the value associated with peanut_butter by passing my variable to the id() function.

I can also put more than one label on the jar:

And check it’s location in memory:

We can assign variables to other variables. Let’s change the value of crunchy first:

Watch what happens if we change crunchy back to 5.

Our variable peanut_butter didn’t change. That’s because when we assigned it to crunchy, like this:

We passed it the value associated with crunchy, which was 7. Where on the shelf is our storage jar of 7?

Let’s say I have five scoops of peanut butter in my storage jar:

Jelly is usually stored in a smaller container, so let’s say I have three scoops:

How many scoops of peanut butter and jelly do I have?

Let’s assign that statement to a variable.

All of these food metaphors are getting me hungry. Let’s make a sandwich. If we use two scoops of peanut butter and one scoop of jelly, we need to assign new values to our variables with the subtraction operator like this:

Now how much peanut butter and jelly do we have left?

Delicious! When you are hungry for more learn all about strings in the next chapter…

 

Hands-on Computer Science

Learn computer science, programming, and web development with us, your educational and entertaining super friends.

Subscribe to our YouTube Channel

Python Variable Assignment and Memory Location was posted by on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.

Discuss this article in the Comments below, or and it with your friends. Learn more about Python by subscribing to The Hello World Newsletter .

Join the Discussion
  • Jan

    Hi – I have tried this in python and the final stage ‘How much peanut butter do we have left?’ using pbj gives a result of 8 and not 5 as demonstrated in your example
    pb + j = 5 what have I not done?

  • Oops. Typo. While robots are perfect, they still make mistakes. I made the correction. Thanks for catching that. And apologies for the late response. I was recharging my batteries.

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid
×