r/PythonLearning • u/Sea-Ad7805 • May 18 '26
Python Data Model exercise for right metal model for Python data
An exercise to help build the right mental model for Python data.
The โSolutionโ link visualizes execution and reveals whatโs actually happening using ๐บ๐ฒ๐บ๐ผ๐ฟ๐_๐ด๐ฟ๐ฎ๐ฝ๐ต.
2
u/Green-Lobster1887 May 19 '26
The answer ic C?
1
u/Sea-Ad7805 May 19 '26
Nice one, do check the "Solution" link for a visualization of the correct answer.
2
u/GRAY_WHALE_CO May 20 '26
its c?
1
u/Sea-Ad7805 May 20 '26
Nice one, see the "Solution" link for a visualization of the correct answer.
1
u/Java_Worker_1 May 18 '26
I think itโs A
2
u/Sea-Ad7805 May 18 '26
Incorrect sorry. Python uses reference semantics so changing
bcan affectahere. See the "Solution" link for the correct answer.
1
u/Motor_Raspberry_2150 May 18 '26
From the explanation
b += [1] # equivalent to: b.append(1)
Is there a difference between b += x a shorthand for append but b = b + x isn't?
1
u/Sea-Ad7805 May 18 '26
Yes, for mutable types there is a big difference, see: https://github.com/bterwijn/memory_graph#name-rebinding
2
u/Motor_Raspberry_2150 May 18 '26
Wow. Unintuitive.
I don't know python at all, reddit algo at work.1
u/Sea-Ad7805 May 18 '26
Yes a bit tricky for beginners, but once you have the right mental model it becomes natural. Different programming languages make different choices here.
1
u/QueasyNight8415 May 19 '26
Memory-Graph is not running the code in the link
1
u/Sea-Ad7805 May 19 '26
It should work on most browsers (Chrome, Firefox, Safari, Android, iPhone), but some plugins or settings can break it. Alternatively you can install memory_graph locally and run it in Visual Studio Code or other IDEs: https://github.com/bterwijn/memory_graph#debugging
1
1
1
u/city_guys May 19 '26
D is the correct answer ๐
1
u/Sea-Ad7805 May 19 '26
Close but incorrect, sorry. Note that x = x + y is not the same as x += y for values of mutable type. See "Solution" link for correct answer.
1
u/Non_Earthy7 May 19 '26
Current b vale: [[1], [2]] Current b vale: [[1, 11], [2]] Current b vale: [[1, 11], [2], [3]] Current b vale: [[1, 11], [2, 22], [3, 33]]
Step wise Output of b
2
u/Sea-Ad7805 May 19 '26
In the end we print
a, we don't really care aboutbexcept how it affectsa.1
u/Non_Earthy7 May 20 '26
Yes i know that, its about a... But for learning purposes i have given b value in each step So that people can understand how the operation is going on
2
u/Sea-Ad7805 May 20 '26
Thanks, but also try the "Solution" link for a visualization of the full state of the program. That probably gives a better understanding.
1
u/Green-Lobster1887 May 19 '26
OMG IT A
1
u/Sea-Ad7805 May 19 '26
Answer 'A' is incorrect, sorry. See the "Solution" link for the correct answer.
1
u/aaditya_0752 Jun 11 '26
Answer is C right ?
1
u/Sea-Ad7805 Jun 11 '26
Nice one, click the "Solution" for a visualization of the correct answer. If you are on mobile, click the title not the image to open a post.
1
May 19 '26
[removed] โ view removed comment
1
u/Sea-Ad7805 May 19 '26
Close but incorrect, sorry. Note that
x = x + yis not the same asx += yfor values of mutable type. See "Solution" link for correct answer.
1
4
u/nicodeemus7 May 18 '26
The answer is C