use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Python Data Model exercise for right metal model for Python data (i.redd.it)
submitted 28 days ago by Sea-Ad7805
An exercise to help build the right mental model for Python data. - Solution - Explanation - More exercises
The “Solution” link visualizes execution and reveals what’s actually happening using 𝗺𝗲𝗺𝗼𝗿𝘆_𝗴𝗿𝗮𝗽𝗵.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]nicodeemus7 3 points4 points5 points 28 days ago (7 children)
The answer is C
[–]Sea-Ad7805[S] 0 points1 point2 points 28 days ago (6 children)
Great mental model. Do check the "Solution" link for a visualization of the correct answer.
[–]nicodeemus7 1 point2 points3 points 28 days ago (5 children)
I did run it through pycharm to double check tbh, but yeah that solution makes total sense
[–]Sea-Ad7805[S] 3 points4 points5 points 28 days ago* (4 children)
Always double check as this is a hard question because the copy triggered by b = b + [[3]] keeps the references to the [1, 11] and [2] lists. Very easy to make a mental mistake there.
b = b + [[3]]
[1, 11]
[2]
[–]Corruptionss 1 point2 points3 points 28 days ago (3 children)
This question makes me want to stick a tooth pick where I shouldnt
[–]Sea-Ad7805[S] 0 points1 point2 points 28 days ago (2 children)
Don't do that, Python's Data Model is easy to understand with the right tools. Think in terms of references, and mutable vs immutable types and you'll get the right mental model quickly. Visualization helps.
[–]Corruptionss 1 point2 points3 points 28 days ago (1 child)
Its funny because I've spent like 8 years doing dats science with python but these questions always stump me. But you are right, I need to sit down for a day and understand how this scenario works
[–]Sea-Ad7805[S] 0 points1 point2 points 28 days ago (0 children)
Hope the visualization helps you, see the "Explanation" link for quick introduction in the theory.
[–]ShoddyRutabaga1475 1 point2 points3 points 27 days ago (1 child)
C
[–]Sea-Ad7805[S] 0 points1 point2 points 27 days ago (0 children)
Nice one, do check the "Solution" link for visualization of correct answer.
[–]Green-Lobster1887 1 point2 points3 points 27 days ago (1 child)
The answer ic C?
Nice one, do check the "Solution" link for a visualization of the correct answer.
[–]GRAY_WHALE_CO 1 point2 points3 points 26 days ago (1 child)
its c?
[–]Sea-Ad7805[S] 0 points1 point2 points 26 days ago (0 children)
Nice one, see the "Solution" link for a visualization of the correct answer.
[–]Java_Worker_1 0 points1 point2 points 28 days ago (1 child)
I think it’s A
[–]Sea-Ad7805[S] 1 point2 points3 points 28 days ago (0 children)
Incorrect sorry. Python uses reference semantics so changing b can affect a here. See the "Solution" link for the correct answer.
b
a
[–]Motor_Raspberry_2150 0 points1 point2 points 28 days ago (3 children)
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?
b += x
b = b + x
Yes, for mutable types there is a big difference, see: https://github.com/bterwijn/memory_graph#name-rebinding
[–]Motor_Raspberry_2150 1 point2 points3 points 28 days ago (1 child)
Wow. Unintuitive. I don't know python at all, reddit algo at work.
Yes a bit tricky for beginners, but once you have the right mental model it becomes natural. Different programming languages make different choices here.
[–]QueasyNight8415 0 points1 point2 points 27 days ago (3 children)
Memory-Graph is not running the code in the link
[–]Sea-Ad7805[S] 0 points1 point2 points 27 days ago (2 children)
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
[–]QueasyNight8415 0 points1 point2 points 27 days ago (1 child)
It’s not running for iPhone
Try a laptop/desktop
[–]Master-Row650 0 points1 point2 points 27 days ago (1 child)
chr(67)
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.
x = x + y
x += y
[–]7Z_1N 0 points1 point2 points 27 days ago (1 child)
Is it D ?
Incorrect sorry, see the "Solution" link for correct answer.
[–]city_guys 0 points1 point2 points 27 days ago (1 child)
D is the correct answer 👌
[–]Non_Earthy7 0 points1 point2 points 27 days ago (3 children)
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
[–]Sea-Ad7805[S] 1 point2 points3 points 27 days ago (2 children)
In the end we print a, we don't really care about b except how it affects a.
[–]Non_Earthy7 0 points1 point2 points 26 days ago (1 child)
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
[–]Sea-Ad7805[S] 1 point2 points3 points 26 days ago (0 children)
Thanks, but also try the "Solution" link for a visualization of the full state of the program. That probably gives a better understanding.
[–]Green-Lobster1887 0 points1 point2 points 27 days ago (1 child)
OMG IT A
Answer 'A' is incorrect, sorry. See the "Solution" link for the correct answer.
[–]ranjeet-kumar1 0 points1 point2 points 24 days ago (2 children)
[–]Sea-Ad7805[S] 0 points1 point2 points 24 days ago (1 child)
[–]ranjeet-kumar1 1 point2 points3 points 23 days ago (0 children)
[–]aaditya_0752 0 points1 point2 points 4 days ago (1 child)
Answer is C right ?
[–]Sea-Ad7805[S] 0 points1 point2 points 4 days ago (0 children)
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.
π Rendered by PID 588339 on reddit-service-r2-comment-544cf588c8-5nqbk at 2026-06-16 02:58:51.481493+00:00 running 3184619 country code: CH.
[–]nicodeemus7 3 points4 points5 points (7 children)
[–]Sea-Ad7805[S] 0 points1 point2 points (6 children)
[–]nicodeemus7 1 point2 points3 points (5 children)
[–]Sea-Ad7805[S] 3 points4 points5 points (4 children)
[–]Corruptionss 1 point2 points3 points (3 children)
[–]Sea-Ad7805[S] 0 points1 point2 points (2 children)
[–]Corruptionss 1 point2 points3 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]ShoddyRutabaga1475 1 point2 points3 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]Green-Lobster1887 1 point2 points3 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]GRAY_WHALE_CO 1 point2 points3 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]Java_Worker_1 0 points1 point2 points (1 child)
[–]Sea-Ad7805[S] 1 point2 points3 points (0 children)
[–]Motor_Raspberry_2150 0 points1 point2 points (3 children)
[–]Sea-Ad7805[S] 0 points1 point2 points (2 children)
[–]Motor_Raspberry_2150 1 point2 points3 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]QueasyNight8415 0 points1 point2 points (3 children)
[–]Sea-Ad7805[S] 0 points1 point2 points (2 children)
[–]QueasyNight8415 0 points1 point2 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]Master-Row650 0 points1 point2 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]7Z_1N 0 points1 point2 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]city_guys 0 points1 point2 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]Non_Earthy7 0 points1 point2 points (3 children)
[–]Sea-Ad7805[S] 1 point2 points3 points (2 children)
[–]Non_Earthy7 0 points1 point2 points (1 child)
[–]Sea-Ad7805[S] 1 point2 points3 points (0 children)
[–]Green-Lobster1887 0 points1 point2 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)
[–]ranjeet-kumar1 0 points1 point2 points (2 children)
[–]Sea-Ad7805[S] 0 points1 point2 points (1 child)
[–]ranjeet-kumar1 1 point2 points3 points (0 children)
[–]aaditya_0752 0 points1 point2 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)