r/PythonLearning • • May 18 '26

Python Data Model exercise for right metal model for Python data

Post image

An exercise to help build the right mental model for Python data.

The โ€œSolutionโ€ link visualizes execution and reveals whatโ€™s actually happening using ๐—บ๐—ฒ๐—บ๐—ผ๐—ฟ๐˜†_๐—ด๐—ฟ๐—ฎ๐—ฝ๐—ต.

121 Upvotes

41 comments sorted by

4

u/nicodeemus7 May 18 '26

The answer is C

1

u/Sea-Ad7805 May 18 '26

Great mental model. Do check the "Solution" link for a visualization of the correct answer.

2

u/nicodeemus7 May 18 '26

I did run it through pycharm to double check tbh, but yeah that solution makes total sense

4

u/Sea-Ad7805 May 18 '26 edited May 18 '26

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.

2

u/Corruptionss May 18 '26

This question makes me want to stick a tooth pick where I shouldnt

1

u/Sea-Ad7805 May 18 '26

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.

2

u/Corruptionss May 18 '26

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

1

u/Sea-Ad7805 May 18 '26

Hope the visualization helps you, see the "Explanation" link for quick introduction in the theory.

2

u/ShoddyRutabaga1475 May 19 '26

C

1

u/Sea-Ad7805 May 19 '26

Nice one, do check the "Solution" link for visualization of correct answer.

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 b can affect a here. 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

u/QueasyNight8415 May 19 '26

Itโ€™s not running for iPhone

1

u/Sea-Ad7805 May 19 '26

Try a laptop/desktop

1

u/7Z_1N May 19 '26

Is it D ?

1

u/Sea-Ad7805 May 19 '26

Incorrect sorry, see the "Solution" link for correct answer.

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 about b except how it affects a.

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

u/[deleted] May 19 '26

[removed] โ€” view removed comment

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/[deleted] May 22 '26

[removed] โ€” view removed comment

1

u/Sea-Ad7805 May 22 '26

Nice one, do check the "Solution" link for visualization of correct answer.