Python Variables

Let’s dive into and check how to use variables in python. Example to start with:

a = 10
  • a is a variable name
  • = is assignment operator
  • 10 is a variable value

Assignment operator differs from one, that is used in algebra. It means that variable points to a memory block, that contains integer 10.

If we use this variable in a print function, we will see 10 in an output:

print(a) # prints 10

Let’s change value of a

a = 5

Now if you decide to print the variable again, you will see 5 in output.

Let’s add one more variable to the program:

b = 20

Now we have two variable that point to two different memory blocks in computer:

Now let’s assign one variable to another and print output:

a = b
print(a)
print(b)

The output for the code above will be 20, 20 and the memory layout would look like:

a and b are stored independently.

Follow-up question:

What will happen to a and b if the next line of code will contain a = 100? Try to guess and then to print output.

Leave a comment