5. Tuples, Lists, Aliasing, Mutability, and Cloning

Поділитися
Вставка
  • Опубліковано 14 лют 2017
  • MIT 6.0001 Introduction to Computer Science and Programming in Python, Fall 2016
    View the complete course: ocw.mit.edu/6-0001F16
    Instructor: Dr. Ana Bell
    In this lecture, Dr. Bell introduces compound data types, such as lists and tuples, and explains the concepts of aliasing, mutability, and cloning.
    License: Creative Commons BY-NC-SA
    More information at ocw.mit.edu/terms
    More courses at ocw.mit.edu

КОМЕНТАРІ • 151

  • @frankhuang5095
    @frankhuang5095 3 роки тому +21

    Thank you MIT for providing such invaluable content to the public! Thank you!

  • @user-fl7vs4ed6l
    @user-fl7vs4ed6l 3 роки тому +30

    pre labeling
    ​​0:01:27 Content of Lecture05
    0:02:32​ New data type : Tuple
    0:04:04 tuple slicing
    0:05:35 tuple swapping 0:08:00
    0:09:09 Manipulating tuples 0:13:00 ​0:12:00
    0:15:31​ New data type : List 0:16:00 0:18:00
    0:19:43 Operation on list 0:20:00 ​0:22:00
    0:23:15 Operation on list
    0:26:55 convert list to string and back 0:28:00​
    0:29:16 Operation on list
    0:31:52 ​An analogy
    0:34:04 idea of Aliasing ​
    0:36:54 Idea of Mutate
    0:38:45​ Nested List
    0:40:17 Mutation and iteration try this python tour

  • @JacobKrajewski
    @JacobKrajewski 5 років тому +19

    The person that edited this video did not do as well as the other, previous lectures. When she is literally pointing at the board, the person kept showing us video of Ana, as she is directing attention away from her to the slides... And then didn't show the slides after she was done talking. The other videos were all done really well by contrast.

  • @leixun
    @leixun 4 роки тому +59

    *My takeaways:*
    1. Introduce compound data type 1:05: tuple and list.
    2. String of a sequence of character data, tuple is a sequence of data in any type 1:35 and is immutable meaning that we cannot change it once we created it.
    3. Why we want to use tuple 5:20.
    4. List is a sequence of data in any type 14:55 and it mutable.
    5. Aliases 34:00.
    6. Cloning 35:15.
    7. Sorting lists .sort() vs sorted() 36:30.

  • @percih70
    @percih70 6 років тому +9

    Python Tutor is such a good idea, it really helps show the concepts, well worth trying.

  • @kararkhan9822
    @kararkhan9822 4 роки тому +6

    Thank you MIT , I am a non-IT student and understood everything.You are doing a great Job!! So far completed 2 courses taught by MIT online learned Alot ;)

  • @user-pe9qg3hg3k
    @user-pe9qg3hg3k 2 роки тому

    Dr Ana Bell is absolutely phenomenal at what she does. What an amazing explanation again. I can't believe MIT has such high quality content up there available for free.

  • @aritrachatterjee8057
    @aritrachatterjee8057 5 років тому +11

    Thank you, Dr. Ana for making OOPs and Inheritance so simple. I could not get concept at first when i was going through MIT 6.00. But now i am equipped with the understanding and assignment problem(6.00) is "walk in the park ".
    Thank You. May Cosmos bless you with more knowledge...

  • @Iain.G.D.B
    @Iain.G.D.B 2 роки тому +2

    Great tutor and lessons however I would like a picture in picture of her and to be able to see what she is pointing at on the screen. hard to follow when you cannot see what she is pointing at.

  • @Liaomiao
    @Liaomiao 6 років тому +244

    camera could really focus on the slides a lot more

    • @garthhh
      @garthhh 6 років тому +34

      You can download them in PDF format from the link in the description and follow along that way

    • @l_combo
      @l_combo 6 років тому +5

      or do picture in picture, you get that when you do the EDX version of this course (also free but more structured)

    • @911Dagur
      @911Dagur 6 років тому +25

      I assume she is talking about when the teacher points at the slides to show the students something with her cursor/laser pen and the camera stays on the professor. Makes it kind of hard to be fully aware of what she is referring to when she says "right here" or "over there".
      They should rather film the slides more to show what she points at on the slides.

    • @marklvrd
      @marklvrd 5 років тому +10

      There is really no point to looking at the Professor, unless they are using the board.

    • @jetspray3
      @jetspray3 5 років тому +14

      Cameraman did a really poor job.

  • @jasonzhang6534
    @jasonzhang6534 10 місяців тому

    Thanks for the video. It really helps me to fully understand the difference between a tuple and a list.

  • @akbarrauf2741
    @akbarrauf2741 7 років тому +13

    thank you ,mit

  • @jaimemontero234
    @jaimemontero234 2 роки тому

    Good lectures , clear slides and Dr. Ana Bell very , very good professor .
    Tanks MIT to give us thats extraordinarys open courses .

  • @aritrachatterjee8057
    @aritrachatterjee8057 5 років тому +3

    Thank you, Dr. Ana for making this so simple. I could not get concept at first when i was going through MIT 6.00. But now i am equipped with the understanding and assignment problem(6.00) is "walk in the park ".
    Thank You. May Cosmos bless you with more knowledge...

  • @TheFootballPlaya
    @TheFootballPlaya 3 роки тому +9

    great video. python, while it's syntax is simple, that simplicity in some ways serves to detract from what is actually happening and how things are processed. It's more apparent when you compare python operations to something like that of c++, where you realize how much python does for you. Other than information hiding, it's a lovely language.

  • @woltron4o
    @woltron4o 6 років тому +3

    I like this lectures. High level of education. Thanks :)

  • @MrFaiqueShakil
    @MrFaiqueShakil 4 роки тому

    So much concept in this lecture.

  • @btvaalburg
    @btvaalburg 4 роки тому +1

    This prof is amazing!

  • @joost3732
    @joost3732 2 роки тому +1

    Ana explains all the theory very clear. Already knew most of the python she explains but it still helped me understand the big picture of programming

  • @russellfatemi4616
    @russellfatemi4616 2 роки тому

    Thank you professor Bell

  • @rajeswarynarasimman3728
    @rajeswarynarasimman3728 6 місяців тому

    Very helpful video indeed. Please show the slide always. Preferably, the slide and the professor together, maybe as picture in picture.

  • @hamids4550
    @hamids4550 6 років тому +98

    camera needs to focus more on the board so we can see what she's talking about rather than just seeing her without the content. It's annoying sometimes

    • @Noldy__
      @Noldy__ 6 років тому +2

      Click on the link in the description. The lecture pdfs are there and you can use them to follow along.

    • @tripleplay4
      @tripleplay4 6 років тому +9

      Yes but we can't see what she is drawing or pointing at in that case. Camera should at a minimum be on the slides when she is looking at the slides.

    • @bee_irl
      @bee_irl 4 роки тому +3

      @@tripleplay4 She is pointing with a laser pointer usually. For us to be able to see that, there would need to be a second camera that focuses only on the board (it would be too much movement for this single camera), and even then, we could barely see the laser dot. I don't find it very hard to follow along on the pdf.

  • @kamranasif212
    @kamranasif212 6 років тому

    Very informative

  • @sapphirelovespj
    @sapphirelovespj 6 років тому +8

    When she's talking about the contents on the projector screen, can you show more of the screen?

    • @Noldy__
      @Noldy__ 6 років тому +1

      Click on the link in the description. The lecture pdfs are there and you can use them to follow along.

  • @Saganist420
    @Saganist420 7 років тому +9

    I really love her subtle humor.

  • @infinitasfish5499
    @infinitasfish5499 2 роки тому

    Thank You very much!

  • @CrushOfSiel
    @CrushOfSiel 3 роки тому +1

    Darn, a lot of this information would have been great to do the hangman assignment! I did it all with strings and it was a bit challenging and probably hard for another programmer to follow in some areas. Lists are awesome.

    • @akshayalva3879
      @akshayalva3879 2 роки тому

      yeah man, I'm still trying to do that, as I'm watching this vid

    • @peasant7214
      @peasant7214 2 роки тому

      well I used ' '.join function at the assignment before I watch this lecture

  • @911Dagur
    @911Dagur 6 років тому +2

    I love that you put these lectures out for everyone to see. But wouldnt it be better to be able to see what the professor is pointing at on the slides? Makes it hard to understand what she is talking about when you can't see what she is referring to.

    • @InventTwig
      @InventTwig 6 років тому +1

      visit the ocw website for the ppts

  • @nancylu4049
    @nancylu4049 3 роки тому +3

    Wonderful! learned a lot! (It would be great if the camera man focused on the slides when Dr.Bell was pointing at them.)

  • @yovelavitan8854
    @yovelavitan8854 5 років тому +2

    on the last problem you can search l2 instead of l1 as shown below:
    def k(l1,l2):
    for n in l2:
    While( n in l1):
    l1.remove(n)

    • @jhw8685
      @jhw8685 5 років тому

      u cant look into l2 and remove from l1 instead. the remove function only removes the first occurrence of the element n in the list, not all element n.
      l1 = [1, 1, 2, 3, 4]
      l2 = [1, 7, 9, 7, 0]
      u will end up with l1 being [1, 2, 3, 4] and l2 stays as [1, 7, 9, 7, 0] and there is still a duplicate

    • @yovelavitan8854
      @yovelavitan8854 5 років тому +1

      @@jhw8685 thank you for the correction, I now changed the code above and I think it's correct now

  • @Pkmafffy
    @Pkmafffy 6 років тому

    I have two questions:
    1. How do I create a tuple, e.g. ((1, a) (2, b) (3, c)), without manually inputting values?
    2. I can't figure out how to return an updated list from a function. Every time said function takes in a new input, all old values of local variables are forgotten and a new list is returned instead of an updated one.
    These questions are in regard to problem sheet 2 - Hangman.
    Any advice would be greatly appreciated!

    • @EgeOnatDoguslu
      @EgeOnatDoguslu 6 років тому

      github.com/egeonatdoguslu/MIT-Assignments/blob/Hangman/Hangman
      I completed the assignment. It might not be the most efficient one for now but, its working. Im sharing that it could give you some clue.

  • @HealthyFoodBae_
    @HealthyFoodBae_ 3 роки тому

    Thanks

  • @lanhaibibolanhaibibo1461
    @lanhaibibolanhaibibo1461 7 років тому +1

    great lecture. Was just wondering, which software do you use to create the lecture slides?

  • @RamkrishanYT
    @RamkrishanYT 7 років тому +16

    lol ...when she says she's being modest about Justin

  • @jetspray3
    @jetspray3 4 роки тому +2

    The camera focus screwed up the whole thing but even though the doctor is good at teaching.

  •  3 роки тому

    29:25 sort list and reverse list

  • @loptrfawkes2149
    @loptrfawkes2149 Рік тому

    I'll make a comment on it, since for some reason this really confused me: why .sort() returns none. (If I get something wrong, feel free to correct me and I will edit this comment, I am new to coding.)
    The reason that list.sort() and list.reverse() return None, while sorted(list) returns a list's values, is basically that the methods are completed by different means. Code that returns None is completed "in-place," which means that the original variable itself is not duplicated, only changed. Python does not want you to think that a new variable has been/could be created after running the method .sort() or .reverse() (or any other in-place method), and so it returns None. It is basically telling you that any method that is done in-place CANNOT generate a new variable because it has not duplicated the old variable.
    Meanwhile, sorted(list) is creating a new list entirely, and so you can assign it to a new variable name. ( sortedList = sorted(originalList) )
    So basically, don't assign in-place methods to any new variables.

  • @UnmannedsRandomStuff
    @UnmannedsRandomStuff Рік тому +1

    no idea what is going on when using laser pointer to point at the physical screen where couldn't see in this video.

  • @dhruv_aditya
    @dhruv_aditya 2 роки тому

    Thank you mit open course to provide us wonderful lectures .
    🇺🇸 ❤ 🇮🇳

  • @edderiofer
    @edderiofer 3 роки тому

    In that last example, couldn't one iterate through the list backwards?

  • @imas1239
    @imas1239 3 роки тому

    I ( we ) need a course from u just to define all these different words you use in most of ur analysis? ???

  • @RyanScarbrough
    @RyanScarbrough Рік тому

    Thanks for this! Mutable side effects are annoying! x_x

  • @yongshengtay8016
    @yongshengtay8016 3 роки тому

    Comparing the current course available on edx and this, I still prefer the full lecture length videos from Fall '16. It's a lot more compact but it's easier to stay focused.

    • @vidieo__
      @vidieo__ 3 роки тому

      ikr. i tried the edx course but i honestly still prefer a recording of an actual classroom! the only downside is that you don't know how well you do on the problem sets.

  • @senanurarslan2323
    @senanurarslan2323 5 років тому +6

    i wish i could see the slide

    • @deeptadevkota5106
      @deeptadevkota5106 5 років тому +1

      You can download the slides using the link provided in the description.

  • @briannestor5125
    @briannestor5125 6 років тому

    I am curious as to the advantage of using a tuple over a list. It seems as though lists have the exact same functionality as tuples but with the added advantage of you being able to modify them(mutability) if you so choose to. Why not just alway use lists?

    • @rhodesclosed9563
      @rhodesclosed9563 6 років тому +2

      I believe lists use more system memory

    • @raphaelcrespo3725
      @raphaelcrespo3725 5 років тому +1

      it an option for a 'list' that you wanna make sure that doesn't change along the code.

    • @geethabr374
      @geethabr374 5 років тому +3

      Because lists may change while code execution and cause unintended output generation. But tuples never modify and that is the added advantage.

    • @kobbyfynn8043
      @kobbyfynn8043 3 роки тому

      Since lists are mutable, they cant be used as keys in a dictionary, but tuples can. So that's one reason to use tuples over lists sometimes

  • @liza10o
    @liza10o 5 років тому

    What does that \ do after max year?

  •  3 роки тому

    22:00 .extend, concatenate, delete, remove, etc...

  • @zcodingkid4749
    @zcodingkid4749 Рік тому

    at 19:19 a list was not created yet. Can someone explain how this works

  •  3 роки тому

    5:28 what are they useful for

  • @najsh2
    @najsh2 2 роки тому

    For the last L1, L2 question, I've tried it with the first code but got L1 = [2, 3]. Is that because Python has been updated to fix that issue??

    • @wilcohermens3492
      @wilcohermens3492 2 роки тому

      Tried the code myself, it didn't work. Maybe you have changed the order of the list? Or did you assign a new list and didn't rerun the code with the original list? Besides that, python has a more simple way to remove the duplicates, try the following:
      L1=[1,2,3,4]
      L2=[1,2,5,6]
      L1=[ e for e in L1 if e not in L2]
      print(L1)

  • @techcorp
    @techcorp 5 років тому +8

    Bad editing. Makes watching this otherwise wonderful lecture a grind.

  •  3 роки тому

    0:45 recap functions

  •  3 роки тому

    15:00 inmutable and mutable

  • @devkiosk
    @devkiosk 6 років тому

    What is the difference between the extend and append functions?

    • @nahfid2003
      @nahfid2003 6 років тому +1

      search on google, it's a popular question

    • @geethabr374
      @geethabr374 5 років тому +4

      Append can add only one element at the end of a list while extend can add multiple elements to an existing list. When you try to add a list at the end of a list using append, it creates ONE element which is again a list and adds it to the list. But extend considers each element like a separate entity and adds it to the list.

  • @peterlin2352
    @peterlin2352 4 роки тому +1

    i have no problem with the camera focus. I think it is great to point to the speaker for her great presentation skill. A good skill to learn for me.

  • @utkarsh22smart
    @utkarsh22smart 4 роки тому

    What is the difference between append and extend of the list.

    • @ficklampa1
      @ficklampa1 4 роки тому +1

      L = [0,1,2,3,4]
      L.append( [101,102,103] )
      L = [0,1,2,3,4, [101,102,103] )
      L[5] is [101,102,103]
      L = [0,1,2,3,4]
      L.extend( [101,102,103] )
      L = [0,1,2,3,4,101,102,103)
      L[5] is 101

  • @obli8984
    @obli8984 2 роки тому +1

    Obli is getting progress

  • @brainstormingsharing1309
    @brainstormingsharing1309 3 роки тому +1

    👍👍👍👍👍

  • @Neil.Menezes
    @Neil.Menezes 8 місяців тому

    Loved the Photo of Justin Bieber being credited to © Justin Bieber in the end 😂

  • @unloadinggnat3396
    @unloadinggnat3396 4 роки тому

    13:25 Who is Joe

  •  3 роки тому

    1:40 tuples

  •  3 роки тому

    2:37 how to represent a tuple

  • @junfan02
    @junfan02 2 роки тому

    I have a confusion, If tuples are immutable how does nums = nums + (t[0],) work?

    • @SameAsAnyOtherStranger
      @SameAsAnyOtherStranger 2 роки тому

      Not sure what context you would use that in, but zero value listed tuples are asking for a return of "false" because that's a boolean if/or/else statement.

    • @avernvrey7422
      @avernvrey7422 Рік тому

      it's concatenating an empty tuple to a singleton tuple (and on and on as the for loop runs). Consider;
      nums = 'walk'
      nums = nums + 'ing'
      print(nums)
      will output walking. 'Walk' and 'ing' are immutable as strings, but they can be concatenated together. In the same way a tuple can be concatenated to a tuple.
      nums = (1,2)
      nums = nums + (3,4)
      print(nums)
      will output (1, 2, 3, 4)
      What it's not doing is changing an element within a tuple, as tuples are immutable.

    • @peterhind
      @peterhind 3 місяці тому

      Tuples are immutable, meaning once they are created, their contents cannot be changed. However, you can create a new tuple by concatenating existing tuples together. This doesn't modify the original tuples but rather creates a new tuple containing elements from both.

  • @aghaaslam9575
    @aghaaslam9575 3 роки тому

    It would be better if the Slide and the lecturer are split in a video

  • @Alikhan-ee7bs
    @Alikhan-ee7bs 5 років тому

    very helpful Could you share the code link and the PPT slide for such kind of code?

    • @mitocw
      @mitocw  5 років тому +1

      Code snippets and lecture notes are available on MIT OpenCourseWare at: ocw.mit.edu/6-0001F16. Best wishes on your studies!

    • @KrishnaDasPC
      @KrishnaDasPC 5 років тому

      I have started a new github site with the code snippets which is currently work in progress. you can have the running examples for the Video 3 here
      pckrishnadas88.github.io/MIT-6.0001-Introduction-to-Computer-Science-and-Programming-in-Python-Fall-2016/#/ch03/README

  • @kumar_vivek764
    @kumar_vivek764 3 місяці тому

    12:38

  • @user-sj4rg2xi1q
    @user-sj4rg2xi1q 7 місяців тому

    I found something wrong which q=0 is right but the r = 4 is not true .

  • @quocvu9847
    @quocvu9847 Рік тому

    10:43

  • @hossainurrahman6700
    @hossainurrahman6700 2 роки тому

    Mam is very beautiful

  • @PasangWangdi
    @PasangWangdi 3 роки тому +2

    MIT need some camera training

  • @bactran9724
    @bactran9724 2 роки тому

    so Python knows to map min_n and max_n to min_year and max_year....?

    • @avernvrey7422
      @avernvrey7422 Рік тому

      as long as the order is correct to what you want. The variable names don't matter, it's the order that matters.

  • @yusufmohammed4452
    @yusufmohammed4452 3 роки тому +1

    That's the only thing I don't like about these videos...when the professor is calling attention to something on the board the camera person just keeps staring at the professor. Can we see what's she's referring to? It's kinda frustrating.

  • @gr8b8m85
    @gr8b8m85 4 роки тому +1

    And this is why university is better than random youtube videos.

  • @DarkellVein
    @DarkellVein 2 роки тому

    So, if you make a variable, equal to another variable, you are creating an alias. So, if var1 = var2, then var1 points to the same memory address as var2, making it a pointer to var2. Coming from c++ it seems weird that when assigning var1 to var2, it makes a pointer, instead of a copy. And instead, having to specify when do you want a copy... But it's ok, as python's thing is being syntactically simple and it also reduces the garbage to collect, etc, etc.

  • @norgileatmonseaur6698
    @norgileatmonseaur6698 3 роки тому +1

    Ah yes, when a concept is being explained the logical course of action is to quickly cut away from the slides.

  • @algemmegla9002
    @algemmegla9002 3 роки тому

    Regarding: 39:56
    Two lists, remove same value - Mutation and Iteration.
    Just thought I would share what I did:
    def remove_dups(L1, L2):
    L1_copy = L1[:] # Make a copy of L1 first, or iteration gets messed up.
    for e in L1_copy:
    if e in L2:
    L1.remove(e)
    print("iteration:", e, "list:", L1)
    # Prints each iteration number, along with the list.

    L1 = [1, 2, 3, 4]
    L2 = [1, 2, 5, 6]
    remove_dups(L1, L2)
    #---------------------------------------------------
    print("", "-" * 30, "")
    # Puts a space/line/space between the two outputs.
    def remove_dups(L1, L2):
    for e in L1:
    if e in L2:
    L1.remove(e)
    print("iteration:", e, "list:", L1)
    # Prints each iteration number, along with the list.


    L1 = [1, 2, 3, 4]
    L2 = [1, 2, 5, 6]
    remove_dups(L1, L2)
    #-------------------------------------------------------------------------------------------
    '''
    Output (you can see where it skips the second iteration):
    iteration: 1 list: [2, 3, 4]
    iteration: 2 list: [3, 4]
    iteration: 3 list: [3, 4]
    iteration: 4 list: [3, 4]
    -
    iteration: 1 list: [2, 3, 4]
    iteration: 3 list: [2, 3, 4]
    iteration: 4 list: [2, 3, 4]
    '''

  • @jyvben1520
    @jyvben1520 11 місяців тому +1

    Sadly we never see where she is pointing to, makes it a bit more difficult to follow, showing her waiving her arm is useless.
    Hope the latest videos produced by MIT fix this (if any), maybe some bright spark can solve it or solved it.
    And give her some rubber bands to keep her shirt sleeves up ?

  • @vigneshr9577
    @vigneshr9577 6 років тому

    Functions and methods are same, isn't it?........

    • @kirankalyan5674
      @kirankalyan5674 5 років тому

      Vignesh Prasanth yep they are 👍

    • @geethabr374
      @geethabr374 5 років тому +3

      No, they are not. Functions are generic. On the other hand, methods are functions that can only work with a specific data type. For example len( ) is a function because it can work on all the series type of data types like strings, tuples, lists. But append( ) is a method because it can only be used with list.

  • @markmilan57
    @markmilan57 9 місяців тому

    I thought python runs from up to down. Your lists mutability shows down to up.

  • @rockren8236
    @rockren8236 9 місяців тому +1

    The lecture is great, but the camera man should be fired. 10:47 Show us what professor is pointing at!!! OMG!

  • @craigdanielmaceacher
    @craigdanielmaceacher 3 роки тому

    FYI don’t use capital “L” as a name, or capital L for anything in python code, it’s not idiomatic python, use a more descriptive name, and avoid capital letters, especially single capital letters. I understand why they did it though, for an example you don’t need a long name and lowercase “l” is hard to discern from the number 1 with a lot of fonts. Get yourself a monospaced programming font like Hack to help!

  • @beeilve
    @beeilve 5 років тому +1

    I feel like she didn't explain aliasing, mutability or cloning clearly.

    • @geethabr374
      @geethabr374 5 років тому

      Refer to John V Guttag's text book. She's using the same and concepts are clearly and concisely explained in the book.

  • @keqi2892
    @keqi2892 4 роки тому +5

    Why Dr.Bell always wears the same clothes every-course?

  • @bikashsubedi2863
    @bikashsubedi2863 2 роки тому

    this video introduced Justin Bieber to me

  • @fwily2580
    @fwily2580 2 роки тому +1

    So why would I pay a ton of money for this?

  • @airbornez602
    @airbornez602 4 місяці тому

    Camera man is an BIG L

  • @buntyyadav7781
    @buntyyadav7781 Рік тому

    Bad way to learn programming...