Variables and Memory Addresses

PythonPythonBeginner
Practice Now

Introduction

In this tutorial, we will explore Python variables, memory addresses, and the differences between mutable and immutable data types:

  • Immutable data types: These data types cannot be changed after creation. Examples include int, float, str, tuple, and frozenset.
  • Mutable data types: These data types can be modified after creation. Examples include list, dict, and set.

With immutable data types, any changes result in a new object being created, while mutable data types can be modified in place. Understanding this distinction is crucial when working with memory addresses and variable assignment.

We will cover variable assignment, the characteristics of mutable and immutable data types, and the is operator for comparing memory addresses. Let's begin!


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-7849{{"`Variables and Memory Addresses`"}} python/with_statement -.-> lab-7849{{"`Variables and Memory Addresses`"}} python/variables_data_types -.-> lab-7849{{"`Variables and Memory Addresses`"}} python/booleans -.-> lab-7849{{"`Variables and Memory Addresses`"}} python/lists -.-> lab-7849{{"`Variables and Memory Addresses`"}} python/standard_libraries -.-> lab-7849{{"`Variables and Memory Addresses`"}} python/data_collections -.-> lab-7849{{"`Variables and Memory Addresses`"}} python/build_in_functions -.-> lab-7849{{"`Variables and Memory Addresses`"}} end

Variable Assignment and Memory Addresses

In Python, variables are just references to memory locations where values are stored. When you assign a value to a variable, Python creates a new object (if necessary) and makes the variable refer to that object.

Open the Python interpreter first:

python

Let's see an example:

x = 10
y = x

In this example, the variable x is assigned the value 10. When we assign x to y, both variables now reference the same memory location. We can check this using the id() function:

print(id(x))  ## Output: memory address of x
print(id(y))  ## Output: memory address of y, which should be the same as x

The is Operator

In Python, the is operator compares the memory addresses of two objects. If the memory addresses are the same, the is operator returns True; otherwise, it returns False. This is different from the == operator, which compares the values of the objects.

x = [1, 2, 3]
y = x
z = [1, 2, 3]

print(x is y)  ## Output: True (x and y have the same memory address)
print(x is z)  ## Output: False (x and z have different memory addresses)

Compare Memory Addresses

In this step, we will compare the memory addresses of variables with the same value.

Please flow the instructions below:

## Create two integer variables `a` and `b` with the same value.
a = 10
b = 10
## Create two list variables `c` and `d` with the same elements.
c = [1, 2, 3]
d = [1, 2, 3]

## Compare the memory addresses of `a` and `b`, as well as `c` and `d`.
print(a is b)  ## Output: True
print(c is d)  ## Output: False

In the example above, we created two integer variables a and b with the same value.

We also created two list variables c and d with the same elements.

When we compared the memory addresses of a and b, the is operator returned True. This is because a and b share the same memory address.

However, when we compared the memory addresses of c and d, the is operator returned False. This is because c and d have different memory addresses.

So, what does this mean?

  1. Immutable data types with the same value may have the same memory address.
  2. Mutable data types with the same value have different memory addresses.

Modifying a Mutable Data Type

Next, we'll create and modify a mutable data type together.

## Create two variables `x` and `y` that reference the same list.
x = [1, 2, 3]
y = x

## Modify the list using variable `x`.
x.append(4)

## Observe the changes in variable `y`.
print(y)  ## Output: [1, 2, 3, 4] (y also reflects the changes because x and y share the same memory address)

In this example, we created two variables x and y that reference the same list.

When we modified the list using variable x, the changes were also reflected in variable y. This is because x and y share the same memory address.

Modifying an Immutable Data Type

Finally, let's work together to modify one immutable data type after another.

## Create two variables `a` and `b` that reference the same string.
a = "hello"
b = a

## Modify the string using variable `a`.
a = a + ", world"

## Observe the changes in variable `b`.
print(b)  ## Output: "hello" (b doesn't change because a new object was created when modifying the string)

In this example, we created two variables a and b that reference the same string.

When we modified the string using variable a, the changes were not reflected in variable b. This is because a and b have different memory addresses.

Summary

In this advanced tutorial, we delved deeper into Python variables and memory addresses. We discussed variable assignment, mutable and immutable data types, and the is operator for comparing memory addresses. We also practiced these concepts with hands-on exercises. Understanding these advanced concepts will help you become a more proficient Python programmer, as it provides greater insight into how Python manages memory and how variables function at a deeper level.

Other Python Tutorials you may like