What is Pointer in Python?
Variables which serve the special purpose of storing the address of other variables, pointing to the address of the variable of which the address is provided, with obtaining of the value stored at the location being called dereferencing, like a page number in the index of a book that drives the reader to the required content, with performance for repetitive operations like traversing data structures being significantly improved, especially in terms of time and space constraints, supporting the copy and access operations effectively, and are indirectly supported as a concept by Python programming language are termed as pointers in Python.
Syntax of Pointer in Python
>>> variable name = value;
Example – 1
>> a = 2
>>> a
>> 2
Example – 2
>>> b = “Bob”
>>> b
>>> Bob
How to Create Pointers in Python?
Below is an example of creating pointers with an isinstance () function to prove that it is an object type. We will see all possible datatypes in Python with isinstance() function; this way, you will learn how to declare all datatypes in python as well.
Example #1
Code:
// assigning an integer value
a = 2
print(a)
// checking if integer is an object or not
print(isinstance(a, object))
// assigning a string value
b = "Bob"
print(b)
// checking if string is an object or not
print(isinstance(b, object))
// assigning a list value
inputList = [1,2,3]
print(inputList)
// checking if list is an object or not
print(isinstance(inputList, object))
//assigning a set value
inputSet = {10,20,30}
print(inputSet)
// checking if set is an object or not
print(isinstance(inputSet, object))
// assigning a tuple value
inputTuple = (100, 200, 300)
print(inputTuple)
//checking if tuple object or not
print(isinstance(inputTuple, object))
// assigning a dictionary value
inputDict = {
"0": 1922,
"1": "BMW",
"2": 100
}
print(inputDict)
//checking if dictionary is an object or not
print(isinstance(inputDict, object))
Output:
Now that we know each variable declared is an object as each isinstance() function return True, meaning that it is an object. Now we can say that everything is an object in Python. Let us learn about mutable objects out of all the objects. Keep in mind that list, set, and dictionary are mutable. Rest are not mutable objects. Mutable objects can be changed, while immutable objects cannot be changed.
Example #2
On an immutable object like a String, we can do an appending as mentioned below:
Code:
str = "Python Programming "
print(str)
print(id(str))
str += "Language"
print(str)
print(id(str))
Output:
and it works, but now if we try to append something else like
Code:
str = "Python Programming "
print(str)
str[5] = “S”
print(id(str))
str += "Language"
print(str)
print(id(str))

4.8 (13,230 ratings)
View Course
Output:
The string throws an error as it is immutable; to modify, we have to use the append() function.
Uses of the Pointer in Python
Pointers are used in C and C++ widely. With Pointers, dynamic memory allocation is possible. Pointers can be declared as variables holding the memory address of another variable.
Pointers Arithmetic Operations
Pointers have four arithmetic operators.
- Increment Operator : ++
- Decrement Operator: —
- Addition Operator : +
- Subtraction Operator : –
Arithmetic operations are performed with the use of arithmetic operators. In the below programs, we have used id() function, which returns the object’s memory address.
Increment operator: It increments the value by 1
Code:
#using the incrementing operator
x = 10
print("x = " ,x, "\n")
print("Address of x", id(x))
x += 1
print("Now x = ",x, "\n")
print(x)
#using the id() function to get the memory address
print("Address of x", id(x))
Output:
Decrementing Operator: It decrements the value by 1
#using the decrementing operator
x = 10
print("x = " ,x, "\n")
print(id(x))
x -= 1
print("Now x = ",x, "\n")
print(x)
#using the id() function to get the memory address
print("Address of x", id(x))
Output:
Addition Operator: It performs the addition of two operands
#using the addition operator
#using the addition operator
x = 10
y = 20
print("x = " ,x, "\n")
print("y = " ,y, "\n")
print("Address of x", id(x))
x = y + 3
print("x = y + 3 \n")
print("Now x = ",x, "\n")
# using the id() function to get the memory address
print("Address of x", id(x))
Output:
Subtraction Operator: It performs subtraction of two operands
Code:
#using the subtraction operator
x = 10
y = 5
print("x = " ,x, "\n")
print("y = " ,y, "\n")
print("Address of x", id(x))
x = y - 3
print("x = y - 3 \n")
print("Now x = ", x, "\n")
print("Address of x", id(x))
Output:
Let us look now with an example using “ is “, which returns true if the objects of both objects have the same memory address.
1. Example
Code:
In this example, we are declaring two variables, x and y, where y is equal to x, which now points to the same memory address as that of x.
x = 100
print("x =", x)
print("address of x", id(x))
y = x
print("y =", y)
print("address of y ", id(y))
Output:
2. Example
In this example, we are declaring two variables x and y, where y is equal to x, which is true, but when we increment the value of y by one, the output turns to be false.
x = 100
y = x
print(y is x)
y = y + 1
print(y is x)
Output:
In the above two examples, we have seen that.
Pointers to Pointers
1. Example
def fun(a, b, c, d):
print(a,b,c,d)
x = (101, 102, 103, 104)
fun(*x)
Output:
2. Example
def fun (a,b,c,d):
print(a,b,c,d)
y = {'a':'I', 'b':'like','c':'python','d':'programming'}
fun(**y)
Output:
3. Example
Putting Example One and Example Two together
def fun (a,b,c,d):
print(a)
print(b)
print(c)
print(d)
x = (100,200,300,400)
fun(*x)
y = {'a':'I', 'b':'like','c':'python','d':'programming'}
fun(**y)
Output:
Conclusion
I hope this article was good enough to make you understand the topics in a better way. Also, the article is self-explanatory to be understood as all the key elements have been explained in the best possible way.
Recommended Article
This has been a guide to Pointers In Python. Here we discuss what pointers is in Python? Different types of pointers and arithmetic operations, along with examples. You can also go through our other suggested articles to learn more –