Member-only story

Python — Understanding Variables and Assignments

Tony
4 min readFeb 9, 2025

Python is a unique language, which is very different from the C language. Many newcomers to Python often say that they do not understand variables and assignments.

Anyone who has learned C knows that when assigning a variable, you need to specify the data type first, and at the same time, it will open up An area of ​​memory used to store values ​​such as:

int a = 1;

Here a is a small area in memory, just like a box in a big room. The assignment in the above statement is to load the integer 1 into the box, like the following picture:

Now let’s reassign the variable a to integer 2:

a = 2;

The box is still the same box, that is to say, the memory address has not changed, but the value in this segment of memory has changed to 2.

Now let’s define another variable b and assign it to a :

int b = a;

When assigning a variable a to another variable b, it is equivalent to copying the value and passing it to the variable b, where b is a newly opened memory area:

--

--

Tony
Tony

No responses yet