Understanding Pointers In Python

In this tutorial, we will learn about Pointers in Python with some cool and easy examples. In some situations, you might have come up with this requirement.

I know you are here because you need this explanation, so you can easily get Pointers in Python.

If you don’t know what are Pointers in Python, then you are in the right place.

Understanding Pointers in Python

First of all, I would like to tell you that, variables we use in Python are Pointers. The equal assignment operator ” = ” automatically creates a pointer in Python except for the case it is basic type variables called an immutable. While referring to basic type variables, I mean int, float, str, bool, these are not Pointers, rather lists, dicts, class objects etc are pointers. Let’s understand these with some examples.

Some Examples

So if you have non-basic type variable ” x ”

x=[2,4,6,8]

And if you assign this x to some variable ” y ” that is,

y = x

Then when you will print y, ” [2,4,6,8] ” will be the output you get but if you change the value of ” x “, without notifying ” y “, you will then get the output which is after the updated value of ” x ” as the output in ” y ” that is,

x=[2,4,6,8] 
y=x
x[0]=10

After considering this if we will print ” y “, we will get ” y ” as ” [10,4,6,8] ” as output. This shows us the example of a Pointer.

Whereas on the other hand when we will consider ” x ” to be 10 which comes under the basic category of variable as explained earlier and does the following changes:

x=10
y=x
x=100

We will still get ” y ” to be the integer value of 10 as output.

This shows the concept of Pointers in Python with respect to basic and non-basic variables.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *