Use a mutable default value as an argument in Python

In this tutorial, you will learn how to use a mutable default value as an argument in Python. The mutable values will be change based on data usage. These mutable default values as arguments to the function call. The data updates in databases and all Websites developed based on Python use the mutable default value as an argument in Python.

Types of Mutable structure:

  1. List []
  2. dictionary {}
  3. set{}

List[]:

The list is a data structure the data in the list are mutable the operations update, append, etc.

represented by->[]

Dictionary{}:

The dictionary is a data structure the data in the list are mutable the operations update, append, etc.

represented by ->{}

Set{}:

The set is a data structure the data in the list are mutable the operations remove, etc.

represented by->{}

Based on the above data structure the data will be mutable and the immutable data structure is a tuple.

The immutable data types are int, float, bool, etc.

Mutable and Immutable data type structure:

The below image shows the structure of the Mutable and Immutable data type

Use a mutable default value as an argument in Python

program on mutable default value as an argument in Python

let see a program on mutable values as an argument.

def fun(l):
    for j in range(len(l)):
        print(l[j])
    for j in range(len(l)):
        print(l[j]+10)
l=[1,2,3]
fun(l)

Output:

1
2
3
11
12
13

Example:2

let see a program on mutable values as an argument.

def fun(l):
    for j in range(1,4):
        print(l[j])
l={1:5,2:5,3:5}
fun(l)

Output:

5
5
5

Explanation:

  • From the above codes, we took a list and dictionary as a data structure “l”.
  • Now pass the “l” as the parameters to the method.
  • By the arguments they will be accessed by considering for a loop.

 

Leave a Reply

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