Dec-03-2019, 10:45 AM
Hi,
I am new to Python so please bear me for this simple question but I really could n't find reason for this.
Current Dict: {}
New Dict: {}
Current Dict: {1: {}}
New Dict: {1: {}}
I understand I am assigning new_dict to current so first time the new_dict is also empty dictionary. But after I assign current[1]={}, why the value of new_dict also got changed?
The reason why I am confused is here is a simple variable assignment program
0
0
-------------------------------------------
10
0
The value of 'b' does not get changed even after I assign a=10.
Why there is a different behavior for dictionary
I am new to Python so please bear me for this simple question but I really could n't find reason for this.
current = {} new_dict = current print("Current Dict:",current) print("New Dict:",new_dict) current[1]={} print("Current Dict:",current) print("New Dict:",new_dict)Here is the output of above program.
Current Dict: {}
New Dict: {}
Current Dict: {1: {}}
New Dict: {1: {}}
I understand I am assigning new_dict to current so first time the new_dict is also empty dictionary. But after I assign current[1]={}, why the value of new_dict also got changed?
The reason why I am confused is here is a simple variable assignment program
a=0 b=a print(a) print(b) print('-------------------------------------------') a=10 print(a) print(b)and the output is below
0
0
-------------------------------------------
10
0
The value of 'b' does not get changed even after I assign a=10.
Why there is a different behavior for dictionary