0% found this document useful (0 votes)
7 views9 pages

Python Functions: Mutable vs Immutable

This document discusses the mutable and immutable properties of data objects in Python, explaining how they behave when passed to functions. It provides examples to illustrate that changes to immutable types do not reflect back in the caller function, while changes to mutable types do, unless they are assigned to a new variable. The document concludes with programming tasks for students to practice their understanding of functions and data types.

Uploaded by

rampriyaghosh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Python Functions: Mutable vs Immutable

This document discusses the mutable and immutable properties of data objects in Python, explaining how they behave when passed to functions. It provides examples to illustrate that changes to immutable types do not reflect back in the caller function, while changes to mutable types do, unless they are assigned to a new variable. The document concludes with programming tasks for students to practice their understanding of functions and data types.

Uploaded by

rampriyaghosh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Working with Functions – Python --continue…..

- tutorial_3
Hopefully, all of you go through the previous uploaded materials and you are able to write programs using functions. In
this material we talk about the next part related to functions in Python.

MUTABLE / IMMUTABLE PROPERTIES OF PASSED DATA OBJECTS:-


You know that you can pass values to functions but here we discuss some important things related to pass values to a
function.

The important points are:-

 Python’s variables are not actually storage containers, it actually like memory references; they refer to memory
address where the value is stored actually.
 Depending upon the mutability/immutability of its data type, a variable behaves differently. That is, if a
variable is referring to an immutable type then any change in its value will also change the memory
address it is referring to, but if a variable is referring to mutable type then any change in the value of
mutable type will not change the memory address of the variable.

Figuratively explanation:-

Now, all of you know that integer literals are stored at some predefined locations.
Let, the following literals are stored in following locations as per figure:-

Now, we write the following code in Python:-

x=7  integer variable


mylist = [7, 9]  list

so, from the above code we must say that the variable ‘x’ and mylist[0] both referring same memory address.
i.e. from the above figure  x = 7  stores the memory address of 7  1597072656
mylist[0] = 7 also stores the memory address of 7  1597072656

mylist[1] = 9  stores the memory address of 9  1597072688

Now, if we make the following changes to our code as:


x=x–1
mylist[0] = 8

Then, x now stores the memory address of integer 6 which is 1597072624


mylist[0] now stores the memory address of integer 8 which is 1597072672

Figuratively:-

When you pass values through arguments and parameters to a function, mutability/immutability also plays an important
role:-
Example code 1: passing an immutable type value to a function:-

Output:-

Look at the above output carefully………………..


Now, the conclusion is that the changed value of ‘a’ inside the function does not reflect on the value of ‘a’ in the
main part of the program
Now, you can understand how Python processed an immutable data type when it is passed as argument on the above
example.
Now, another example:-
Example code 2: passing a mutable type value to a function:-

Output:-

Look at the above output carefully………………..


Now, the conclusion is that the changed value of mylist[0] inside the function reflected on the value of mylist[0] in
the main part of the program

So, from the above two examples it is clear that in Example code 1 the variable ‘a’ is an immutable type so it behaving
like pass by value concept (as any changes made within the function do not get reflected back in the main part of
the program) and in Example code 2 the list variable ‘mylist’ is a mutable type object so it behaving like pass by
reference (as any changes made within the function do get reflected back in the main part of the program).

Another example:-
Similarly, if you make other changes such as adding or deleting items from a passed list, these will also get reflected
back as both the passed list argument and received parameter list work with the same memory address where changes
are done in place.
Following are some examples and outputs on that:-
Example: passing a mutable type value to a function – adding/deleting items to it.

Output:-

The above example shows that all changes done in the function part got reflected in the main part of the
program.

Now, another important situation to consider:


look at the following example carefully……………….
Example: Passing a mutable type value to a function – assigning parameters to a new value/variable:-
Output:-

Now, carefully look at the above output……………..


What you see…………………
The value got changed from [11,15] to [101, 201, 66] inside the function and that change did not get reflected in
the main part of the program.
isn’t it?
....................
isn’t this unexpected. the passed value is of a mutable type, a list but still the changes made in the function do not get
reflected back to the main part of the program………..why?
Let’s go through the following figure:

Elements 0 and 1 of ‘mylist’ refer to the addresses of values they hold as on the above figure. You passed the mylist to
the function, so ‘list_my ‘ within the function also refer the same addresses of values as like ‘mylist’. So, both stored in
40116 address as per the above figure.

Now, when a new list named as ‘new_list’ is created for which a new memory allocated, so now the figure is:

So, from the above figure, we conclude that ‘list_my’ and ‘new_list’ both stored in different memory addresses within
the function.
Now, when you assign ‘new_list’ to ‘list_my’ as follows:
list_my = new_list
then ‘list_my’ now points to ‘new_list’ memory address which is 50116 on the figure and the previous address gets
changed.
So, in that point ‘mylist’ in the main part of the program holds the address 40116 and ‘list_my’ and ‘new_list’
within the function holds 50116 address, which are different.
Therefore, whatever changes you have done on ‘list_my’ within function it never be reflected back to the ‘mylist’
of the main part of your program.

Hopefully, now the above output is cleared to you.

Conclusion:-
 Changes in immutable types of data are nor reflected in the caller function at all.
 changes, if any, in mutable types –
 are reflected in caller function if its name is not assigned a different variable or
datatype.
 are not reflected in the caller function if it is assigned a different variable or datatype

Some Examples – that will help you writing program development:


Example: passing list to a function
(Remember:-List is mutable datatype that’s why it treat as pass by reference.)

Output:-
Example: passing string to a function

(Remember:- String can be passed in a function as argument but it is used as pass by value. It can be
depicted from below program. As it will not change value of actual argument.)

Output:-

Example: passing tuple to a function

(Remember:- in function call, we have to explicitly define/pass the tuple. It is not


required to specify the data type as tuple in formal argument. As tuple is immutable so
we extract values from tuple for further operation.)

Output:
Example: passing dictionary to a function

(Remember:- In Python, everything is an object, so the dictionary can be passed as an


argument to a function like other variables are passed. It is mutable type.)

Output:-

Program tasks:-
1. Write a function, which accepts a list of numbers as parameter then calculate the sum of all elements
present in the list and return the addition value from the function.
2. Write a function, which accepts a list of numbers as parameter then find the largest element of the
list and return it from the function.
3. Write a function, which accepts a list of numbers as parameter then find the largest and smallest
element of the list then returns these two elements from the function.
4. Write a function, which accepts an integer list as parameter then rearranges the list in reverse order
and display the list.
5. Write a function, which accepts an integer list as parameter then replaces elements having even
values with its half and elements having odd values with twice its value then display the list
6. Write a function, which accepts an integer list as parameter then divide all those list elements by 7
which are divisible by 7 and multiply other list elements by 3 then display the list.
7. Write a function, which accepts an integer list as parameter having even number of elements then
exchanges the values of first half side elements with the second half side elements of the list and the
display the list.
hints: if a list of eight elements contains: 2 4 1 6 7 9 23 10
the function should rearrange the list as: 7 9 23 10 2 4 1 6
8. Write a function, which accepts an integer list as parameter having even number of elements. Then
the function should rearrange the list in such a way that the values of alternate locations of the list are
exchanged and then display the list.
hints: if the list initially contains : 2 5 9 14 17 8 19 16
then after rearrangement the list should contain: 5 2 14 9 8 17 16 19
9. Write a function, which accepts a list as parameter then search for a particular element in the list, if
the element is present within the list then the function should return 1 otherwise it returns 0.
10. Write a function, which accepts a list as parameter then sort the list elements in ascending order
and display the list.
11. Write a function, which accepts a word (string) as parameter then checks that the word is a
palindrome word or not.
hints: If the original word and the reverse of the word are same then it is called palindrome word. Like
MADAM is a palindrome word.
12. Write a function, which accepts a string as parameter then search for a particular word is present on
that string or not. If he word is present then the function should return 1 otherwise 0.

Students’, ‘Working with Functions’ chapter is


completed………

Read the whole thing carefully and practice it……….

Try to solve all programs……….

If any problem then message me……………….

You might also like