Got Homework?
Connect with other students for help. It's a free community.
Here's the question you clicked on:
 0 viewing
Need help on 6.00 Handout, Lecture 5:
Here are the lists that I am looking at:
1.
L1 = [2]
L2 = [L1, L1]
print 'L2 =', L2
# L2 = [[2], [2]]
2.
L1[0] = 3
print 'L2 =', L2
# L2 = [[3], [3]]
3.
L2[0] = 'a'
print 'L2 =', L2
# L2 = ['a', [3]]
4.
L1 = [2]
L2 = L1
L2[0] = 'a'
print 'L1 =', L1
print 'L2 =', L2
# L1 = ['a']
# L2 = ['a']
I understand everything up to number 4 (correct answers start with a #). I can't understand why L1 = 'a' here. I was expecting L1 to be equal to [2].
Could someone write down an explanation about what is going on in part 4 of the
 one year ago
 one year ago
Need help on 6.00 Handout, Lecture 5: Here are the lists that I am looking at: 1. L1 = [2] L2 = [L1, L1] print 'L2 =', L2 # L2 = [[2], [2]] 2. L1[0] = 3 print 'L2 =', L2 # L2 = [[3], [3]] 3. L2[0] = 'a' print 'L2 =', L2 # L2 = ['a', [3]] 4. L1 = [2] L2 = L1 L2[0] = 'a' print 'L1 =', L1 print 'L2 =', L2 # L1 = ['a'] # L2 = ['a'] I understand everything up to number 4 (correct answers start with a #). I can't understand why L1 = 'a' here. I was expecting L1 to be equal to [2]. Could someone write down an explanation about what is going on in part 4 of the
 one year ago
 one year ago

This Question is Closed

JuanVBest ResponseYou've already chosen the best response.0
L2 and L2 are pointing to the same object in memory  This is why around 30mins in the video the programming keeps running. This is call Aliasing  one object with more then one name.
 one year ago

RoamingBlueBest ResponseYou've already chosen the best response.0
@tcc8m the first line sets L1 equal to a list that's big enough to contain two items. The second line sets L2 equal to L1 which means that they're both "equal to" or "pointing at" the same list. So when the third line sets the first member of the list to the value of 'a', both variables (since they're both pointing at /equal to the same list) get updated.
 one year ago

bwCABest ResponseYou've already chosen the best response.1
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#otherlanguageshavevariables
 one year ago

RoamingBlueBest ResponseYou've already chosen the best response.0
@bwCA that's an awesome link. Thank you for that, I'm bookmarking it now :D
 one year ago

tcc8mBest ResponseYou've already chosen the best response.0
Thanks @RoamingBlue, and @bwCA and @JuanV. Awesome answers! I agree with @RoamingBlue that the link was perfect. The price tags vs. box illustration is genius. Pictures were great too.
 one year ago

JuanVBest ResponseYou've already chosen the best response.0
That is a great example bwCA :)
 one year ago
See more questions >>>
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.