Which Output Is Correct?? #python #programming #coding

preview_player
Показать описание
This short video explains the concept of pointers in python and how a datatype can have multiple variables assigned to it.

Background music:
Creative Commons Attribution 3.0 Unported License
Рекомендации по теме
Комментарии
Автор

This is one of the few places where growing up on Python, rather than a statically-typed OOP language like C++, C#, or Java, can really bite you. This is the classic Value types VS Reference Types dilemma. Like b00l said, in Python, a variable which stores a List is actually holding an ADDRESS that REFERENCES the List in HEAP Memory. If you store that Address in more than one variable, then any changes made to one will affect the others.

WhNeedsFrends
Автор

To get answer B, line 2 should be:
b = a.copy()

amineberouaken
Автор

Absolutely love these little review shorts. Definitely makes me feel better taking a break from studying

viatori
Автор

If you want to create a copy of a list without pointing back to it, try this:

a = [1, 2, 3]
b = a[::]

Now if you write b[2] = 100, it won't change list a

idrawfurries
Автор

just learned this earlier and after I saw the question I already know the answer. as a beginner I felt good

mewwo
Автор

please do more of these, it's a pretty simple practice for newbies

methanesulfonic
Автор

I literally forgot about the index numbers started from 0 rather than 1.. Damn it i felt stupid 😂

DarellCampkynton
Автор

What really helps me understand these types of things is to think of variables as labels rather than containers. First, the list object is created and then the label "a" is put on it (like putting a sticky note with the letter a on the list). After that, another label or "sticky note" (b) is put on it when b = a is run.
You can check that a and b reference to the same object by running "a is b".

breixosanchezmontero
Автор

Quickly got it. I’m learning coding right now and we are using Python to code. It’s really easy and a fun program to work with!

renkujo
Автор

My thinking process is getting better with those type of questions 😁😁

kourygg
Автор

I had this issue come up in a program. You can get around it by writing:

b = list(a)

It will make b a separate list that you can modify without changing a.

ukraineme
Автор

So basically everything is a reference type in python, sounds super memory efficient!!!!

markrobershorts
Автор

That's where deep copy comes in place.

ashutoshpatel
Автор

I like this because it shows that trying to remember if python assignment is by value or by reference makes it easy to miss the array indexing starts at 0.

TheNewton
Автор

It's basically do you know if b = a is a copy or reference/pointer

D_To_The_J
Автор

This is actually pretty funny because you can abuse this to have links in python

vladyslavkryvoruchko
Автор

I wish b = a didn’t function like a pointer. It’s confusing since it also the method to assign a variable

rcr
Автор

Just realized why the little project I gave up on a couple months ago never worked.

BaconMonster
Автор

This is going to be a nightmare for multiple threads since even when you assign the variable to another thread it still shares resources.

jhgvvetyjj
Автор

B should be a seperate variable, not a pointer to A, but I guess that's the dinosaur in me.

TheOtherSteel