Python pass by reference or value with examples

In this Python tutorial, let us discuss on Python pass by reference or value with a few examples.

  • Python pass by reference vs pass by value
  • Python call by reference vs call by value
  • Python pass by reference example
  • Python pass by value example
  • Pass by reference vs value in python
  • Python function arguments pass by reference or value
  • Python pass string by value

Python pass by reference vs pass by value

Pass by reference – It is used in some programming languages, where values to the argument of the function are passed by reference which means that the address of the variable is passed and then the operation is done on the value stored at these addresses.

Pass by value – It means that the value is directly passed as the value to the argument of the function. Here, the operation is done on the value and then the value is stored at the address. Pass by value is used for a copy of the variable.

Call by reference vs call by value

Call by referenceCall by value
While calling a function, in a programming language instead of copying the values of variables, the address of the variables is used, it is known as “Call By Reference.”While calling a function, when we pass values by copying variables, it is known as “Call By Values.”
In this method, a variable itself is passed.A copy of the variable is passed in a call by value.
Change in the variable also affects the value of the variable outside the function.Changes made in a copy of a variable never modify the value of the variable outside the function.
Allows you to make changes in the values of variables by using function calls.Does not allow you to make any changes in the actual variables.
The original value is modified.Original value not modified.

Python pass by reference example

When we pass something by reference any change we make to the variable inside the function then those changes are reflected to the outside value as well.

Example:

student = {'Jim': 12, 'Anna': 14, 'Preet': 10}
def test(student):
    new = {'Sam':20, 'Steve':21}
    student.update(new)
    print("Inside the function", student)
    return 
test(student)
print("Outside the function:", student)

After writing the above code, Once you will print “student” then the output will appear. Here, we created a dictionary called student, and test(student) is the function. Then two more students joined so we created the variable as “new” and the student.update(new) is used to update the dictionary, also the print will display the output.

You can refer to the below screenshot for python pass by reference example

Python pass by reference or value
Python pass by reference example

Python pass by value example

When we pass something by value then the changes made to the function or copying of the variable are not reflected back to the calling function.

Example:

student = {'Jim': 12, 'Anna': 14, 'Preet': 10}
def test(student):
    student = {'Sam':20, 'Steve':21}
    print("Inside the function", student)
    return 
test(student)
print("Outside the function:", student)

After writing the above code, Once you will print “student” then the output will appear. Here, we created a dictionary called student, and test(student) is the function. Then two more students joined so we created the variable as “new” and the print will display the output. We can see that the inside and outside function remains the same.

You can refer to the below screenshot for the python pass by value example

Python pass by value example
Python pass by value example

Pass by reference vs value in python

In the below example, we can see that all the parameters in the python language are passed by reference. So, if we change what a parameter refers to within a function, the change also reflects back in the calling function.

Example:

def marks(list):
    list.append([11, 12, 13, 14, 15])
    print("Value inside the function: ", list)
    return
list = [10,20]
marks(list)
print("Value outside the function: ", list)

In this output, we can see that we are maintaining the reference of the passed object, and values are appending in the same object. So, you can see the output of the inside function and outside function.

You can refer to the below screenshot pass by reference vs value in python

Pass by reference vs value in python
Pass by reference in python

Python function arguments pass by reference or value

The parameters in the python language are passed by reference. Which mean if we change what parameter refers to within the function, the change also reflect black in the calling function.

Example:

teacher = {'Peter':101, 'John':102, 'Suzain':103}
def test(teacher):
   new = {'kat':104, 'Satya':105}
   teacher.update(new)
   print("Inside the function",teacher)
   return
test(teacher)
print("Outside the function:",teacher)

After writing the above code, Once you will print “teacher” then the output will appear. Here, we created a dictionary called teacher, and the def test(teacher) is the function. Then two more teachers joined so we created the variable as “new” and the print will display the output. We can see that the inside and outside function remains the same.

You can refer to the below screenshot python function arguments pass by reference or value.

Python function arguments pass by reference or value
Python function arguments pass by reference or value

Python pass string by value

In this example, we have passed strings to a function, and the string value is an immutable object which is being passed to the function. So, the changes made to the function or copying of the variable are not reflected back to the calling function.

Example:

my_string = "Python"
def test(my_string):
    my_string = "PythonGuides"
    print("Inside the function:",my_string)
test(my_string)
print("Outside the function:",my_string)

In this output, once you will print “my_string” then the output will appear. Here, we created the function called def test(my_string). Here, the passing is like a pass string by the value as we can not change the value of the immutable object.

You can refer to the below screenshot python pass string by value.

Python pass string by value
Python pass string by value

You may like the following Python tutorials:

In this Python tutorial, we have learned about the python pass by reference or value. Also, We covered these below topics:

  • Python pass by reference vs pass by value
  • Python call by reference vs call by value
  • Python pass by reference example
  • Python pass by value example
  • Pass by reference vs value in python
  • Python function arguments pass by reference or value
  • Python pass string by value