PYTHON

Understanding Variables And References In Python

If you don’t sooner or later your code will blow up…

Jacob Ferus
5 min readAug 28, 2022

--

Image generated using OpenAI DALL·E 2.

Let’s start from the beginning by writing down some terminology, what are variables, values and references?

  • Variables are names that point or refer to objects.
  • A reference can be seen as the connection between a variable/name and a value, it contains the memory address where the value is held.
  • A value is another word for the actual data or object.

Okay, but why all this complexity? Why not see everything as the same thing, i.e. a variable is a value and not bother with all of this? The reason is that multiple variables can point to the same value. This means that if you change one of the variables then the value of all variables will change or (rather the value the variables point to). This is very important to understand otherwise your programs will behave unexpectedly. Let’s illustrate these concepts with an example:

--

--

Jacob Ferus

Looking outside the box and making sense of the world using data.