r/PythonLearning 9d ago

Python Data Model Exercise

Post image

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 ๐—บ๐—ฒ๐—บ๐—ผ๐—ฟ๐˜†_๐—ด๐—ฟ๐—ฎ๐—ฝ๐—ต.

66 Upvotes

39 comments sorted by

2

u/Embarrassed-Crow9283 8d ago

C. The first two lists in b were still the same lists as the lists in a, but the third list was added and the third element would only be in b and not a. You appended the list in position 0 with 11 and in position 1 with 22. This gives result C when printing out a.

0

u/Sea-Ad7805 8d ago

Nice one, do check the "Solution" link for a visualization of what you explain here.

2

u/arivictor 7d ago

https://parsnip.dev/editor/qDQDFUs4QrHX

C

At the start b = a does not create a copy, b simply references a. However, as soon as you do b = b + [[3]] you create a new list in b, a is no longer linked to b.

1

u/Sea-Ad7805 6d ago

Nice one, do check the "Solution" link for a visualization of what you explain here.

1

u/Dapper_Bad_8728 9d ago

I think it's D.

1

u/Sea-Ad7805 9d ago

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

1

u/Dapper_Bad_8728 8d ago

Oh yeah it prints the "a" list not the "b" one. I though it was printing the "b"

1

u/Sea-Ad7805 8d ago

Easy mistake to make, no problem.

1

u/eudye 8d ago

A)

1

u/Sea-Ad7805 8d ago

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

1

u/orangejuice1986 8d ago

ignore everything after b = ....

hence B)

1

u/Sea-Ad7805 8d ago

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

1

u/ghost59 8d ago

It's A.

1

u/Sea-Ad7805 8d ago

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

1

u/ghost59 8d ago

That's weird. Why is it printing C when a didn't touch again.

1

u/Sea-Ad7805 8d ago

See the "Explanation" link for an explanation of Python's data model.

1

u/ghost59 8d ago

Oh! Wait. I'm stupid. You didn't make a copy of the values with b. You copied the original and when you change b you mutate a as well. It's because the list is something that can be mutated.

If I wanted a not to mutate. It would need to be B = a.copy()

1

u/Sea-Ad7805 8d ago

Be careful, b = a.copy() makes a shallow copy. Are you sure you want that? https://github.com/bterwijn/memory_graph?tab=readme-ov-file#copying-values-of-mutable-type

2

u/ghost59 8d ago

No. I want a deep copy. Because you're right. Looks like I'll be hitting training grounds on boot.dev tonight.

1

u/Sea-Ad7805 8d ago edited 8d ago

I tried to make the exercise difficult so don't feel bad. Keep the memory_graph tool around to visualize tricky Python situations, also with boot.dev exercises.

1

u/ghost59 8d ago

Honestly. This was fun. I am almost done with go on boot.dev. I'll star the repo. Time to become cracked.

1

u/Samar_Upreti 8d ago

Option C correct Concept of indexing (No index [2] exists) eliminate option D As we know list are mutable in python and we use .append

1

u/OopsAllCharisma 8d ago

What's the line b = b + [3] for?

1

u/Sea-Ad7805 8d ago

Line b = b + [[3]] adds [3] to the b list, and this exercises is about how this relates to a. First think about it, then check the Solution.

1

u/Cheems-sudo 3d ago

i think it's C

1

u/Alireza7074 1d ago

C

1

u/Sea-Ad7805 1d ago

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

1

u/FoolsSeldom 8d ago edited 8d ago

C.

Line 4 re-assigns b to a new list object so severs the link to the original list object referenced by a and b up to that point.

EDIT: More detail for learners regarding variables and lists in Python (on back of a challenge from u/HardyDaytn):

Variables in Python do not contain values, but simply reference where in Python a Python object (int, float, str, list, etc) is located in memory. Generally, you don't need to concern yourself with the memory matters.

A Python list object similarly does not contain any variables or values, but only a collection of zero or more references to other Python objects.

Two variables can reference the same object and if the object is mutable (able to be changed), such as a list, then changing the object can be done via either variable.

Thus,

alpha = 10
beta = 'eleven'
charlie = 12.1
delta = ['mary', 'had', 'flower']

stuff = [alpha, beta, charlie, delta]

This does the following:

  • Assigns a memory reference to the pre-defined int binary object representation of decimal 10 to the variable name alpha
  • Assigns a memory reference to the new str object (consisting of binary unicode numbers for each character in the string) to the variable name beta
  • Assigns the memory reference to the new float binary object representation of the decimal floating point number 12.1 to the variable name charlie
  • Assigns the memory reference to the new list object, containing 3 entries to new str objects, to the variable name delta, where each entry in the list is itself a memory reference to a new Python object
  • Assigns the memory reference to the new list object, containing 4 entries, to the variable name stuff, where each entry in the list is itself a memory reference to a Python object as below,
    • the first entry in the list holds the same memory reference as alpha holds
    • the second entry in the list holds the same memory reference as beta holds
    • the third entry in the list holds the same memory reference as charlie holds
    • the fourth entry in the list holds the same memory reference as delta holds, i.e. they both refer to the same list object

It is worth noting that if you change the contents of the list referenced by delta, that change will also show up in the list referenced by stuff. This is shown in the example below.

Complete code:

alpha = 10
beta = 'eleven'
charlie = 12.1
delta = ['mary', 'had', 'flower']

stuff = [alpha, beta, charlie, delta]
print(stuff)
delta[1] = 'lost'
print(stuff)

Outputs,

[10, 'eleven', 12.1, ['mary', 'had', 'flower']]
[10, 'eleven', 12.1, ['mary', 'lost', 'flower']]

1

u/Sea-Ad7805 8d ago edited 8d ago

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

0

u/FoolsSeldom 8d ago

No thanks, could visualise it virtually (mentally) with ease. Good exercise for learners though.

0

u/Sea-Ad7805 8d ago

Thanks, but are you sure? It's a really powerful visualizer that can help to understand and debug tricky Python situations: https://memory-graph.com/#codeurl=https://raw.githubusercontent.com/bterwijn/memory_graph_videos/refs/heads/main/exercises/exercise18.py&play

1

u/FoolsSeldom 8d ago

Yes. I probably should not have answered the challenge. It was something of a fly-by.

1

u/Sea-Ad7805 8d ago

No problem, thanks for participation anyway.

1

u/HardyDaytn 8d ago

Not a very beginner friendly explanation though, as line 5 is still changing list a.

1

u/FoolsSeldom 8d ago

The OP has provided linked comprehensive explanations including detailed diagrams.

However, if any beginner wants to ask for clarification, I'd be more than happy to expand.

1

u/HardyDaytn 8d ago

The OP has provided linked comprehensive explanations including detailed diagrams.

Right, so instead of trying to be helpful, you only wanted to toot your own horn here.

A healthy dose of "look ma', I know this one too!".

1

u/Sea-Ad7805 8d ago edited 8d ago

It's even worse, it's a "look ma', I've built this tool" situation. The Python data model can seem difficult at first, but I hope the visualization can help beginners understand it more easily. Beginners will have to face it at some point if they want to get to advanced.

1

u/FoolsSeldom 8d ago

So, I've edited the original comment to add additional information for learners if they don't want to follow the links provided by the OP. (This is using repurposed content from classes I've run.)

1

u/HardyDaytn 8d ago

My proverbial hat's off to you for adding detailed info. It's an annoying and difficult concept to learn and understand compared to other more intuitive things.