Expert Python Tutorial #5 - Generators

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ธ.ค. 2024

ความคิดเห็น • 108

  • @PranavArrora
    @PranavArrora 4 ปีที่แล้ว +51

    I am a masters student with major in data science. Though I am familiar with python(mostly) but your insights are amazing and the way you articulate. Really appreciate your content Tim :)
    Thanks so much.

  • @claudiu_ivan
    @claudiu_ivan 3 ปีที่แล้ว +1

    def fibb_gen(n):
    a,b=1,1
    yield 1
    while a

  • @paulzupan3732
    @paulzupan3732 2 ปีที่แล้ว +1

    One tip that I don't see a lot is a method for taking over iteration of another iterable. Let's say you have an iterable out of which you would like to make a generator, such as in the example at 7:31. Instead if manually looping over the iterable and yielding each value individually, you could simply write the following:
    def gen(n):
    yield from range(n)
    It's way more concise. Hopefully that's helpful.

  • @adryelbarros3250
    @adryelbarros3250 4 ปีที่แล้ว +15

    Another way of making a generator is by using () other then [] in the list comprehension:
    a = (i**2 for i in range (100))
    print(a) # < generator at...>
    for i in a:
    print(i)

    • @danielkeehl7450
      @danielkeehl7450 4 ปีที่แล้ว +1

      I was just about to say the same)

    • @jeverydk
      @jeverydk 2 ปีที่แล้ว

      Aint this only under the assumption that data in the variable a is unchangeable afterwards.
      Both a list[] and tuple() should work indifferently as example

    • @adryelbarros3250
      @adryelbarros3250 2 ปีที่แล้ว +1

      @@jeverydk I think I didn't get what you said
      In this example, a is not a tuple(), it's a generator, that is, it's gonna generate the values lazily. If you try that with range(10**9) in a list() or a tuple() you sure gonna run out of memory, whereas using the generator as shown you wouldn't have the memory issue

    • @jeverydk
      @jeverydk 2 ปีที่แล้ว +1

      @@adryelbarros3250 i might have misunderstood your example. My bad

  • @Xaminn
    @Xaminn 3 ปีที่แล้ว

    I enjoy your videos. Not only do you explain how, but you also explain why. You could explain to me how something was built. However, explaining why this method was chosen over a different method, is a whole different level of understanding. 10/10

  • @alexandrumoldovan9936
    @alexandrumoldovan9936 2 ปีที่แล้ว +1

    Great video, as per usual. Tim is really a programming prodigy and a great teacher, making complicated topics seem simple and easy-going. Thank you!
    As a bit of trivia, a useful method for keeping track of large numbers in code is to use the " _ " character for visual delimitation:
    >>> nr = 1_00000_000_000
    >>> print(nr)
    Output: 100000000000.

  • @antoniodsantos
    @antoniodsantos 4 ปีที่แล้ว +11

    Hi Tim. I'm a software tester working in automation, and having a go at intermediate level certification in Python. This is what I needed has complement to Python Institute's course. Thank you for sharing your knowledge! Greetings from Portugal.

  • @gedtoon6451
    @gedtoon6451 4 หลายเดือนก่อน

    At 8:28 you create a generator object 'g' that you then use in a for loop. If you are going to use a generator in a loop, you can write for i in gen(1000000): and miss out creating g.

  • @amreshgiri
    @amreshgiri 3 ปีที่แล้ว +3

    Probably the best explanation out there.

  • @blakebiz2335
    @blakebiz2335 4 ปีที่แล้ว +9

    Best series ever :D thanks Tim!

  • @harmannatsingh3933
    @harmannatsingh3933 4 ปีที่แล้ว +3

    This is such a good series. Keep it up Tim!!

  • @escalatedchi
    @escalatedchi 4 ปีที่แล้ว +1

    Consider using what is called generator expression much like list comprehension, the difference being you get a generator object instead of, well, a list. Just wrap the expression with parentheses instead of square brackets. gen=(x**2 for x in range(10000000)), print(gen.next())

  • @drewmeder5968
    @drewmeder5968 4 ปีที่แล้ว +4

    Thank you! This series is great!!

  • @arimakishou4511
    @arimakishou4511 4 ปีที่แล้ว +17

    Holy smokes, I copied out the code you did in the first two minutes and ran it, my computer made a noise I've never heard before...

  • @augustong8371
    @augustong8371 4 ปีที่แล้ว

    Keep up the series man! I'm a college student as well and most of the time, you are one of the channel which has helped me to continue learning python and self explore projects on the side to build my portfolio!

  • @mampiisaotaku
    @mampiisaotaku 4 ปีที่แล้ว

    wow! now i understand why yield and next are so useful. that wass greatly explained

  • @XRay777
    @XRay777 4 ปีที่แล้ว +2

    You can also create asynchronous programs using generators because you can pause a function with yield and then use the send method of generators to continue execution. Not that this is insanely useful but perhaps nice to know ;)

    • @adasilberstein9455
      @adasilberstein9455 2 หลายเดือนก่อน

      you mean instead of using events?

  • @KING-zz6qt
    @KING-zz6qt 3 ปีที่แล้ว

    Hi, does anyone know why program prints twice more ram used for the same code and same given n as it does at 12:55

  • @riverwang5482
    @riverwang5482 2 ปีที่แล้ว +1

    Thanks! Question: is there a reason to separate next() and __next__()?

    • @DrDeuteron
      @DrDeuteron 2 ปีที่แล้ว

      I think not. Put all the code in dunder next() and call the instance with the built-in next(instance) function, which executes instance.__next__().

  • @patrickwildschut5750
    @patrickwildschut5750 4 ปีที่แล้ว +1

    Very useful, thanks!👌👌

  • @ryankao1983
    @ryankao1983 3 ปีที่แล้ว

    exceptional video

  • @georgettebeulah4427
    @georgettebeulah4427 4 ปีที่แล้ว

    This make so much sense and thank you for your video

  • @nickmadaffari8367
    @nickmadaffari8367 3 ปีที่แล้ว

    Great video! Is there a way to nest generators within other generators? Like nested for loops that create lists, but with generators instead?

  • @debjyotibiswas3793
    @debjyotibiswas3793 4 ปีที่แล้ว

    Amazing series, I hope there are more!

  • @josevelez6865
    @josevelez6865 4 ปีที่แล้ว

    Very helpful. Thanks for sharing. Some follow up questions:
    1. Would I be correct in thinking that underlying all of this is Python utilizing some sort of linked list data structure to know where it currently is within the iterable? Is that how/why the __next__ method is applied?
    2. I noticed when I ran the test on size using the sys.getsizeof() method for the same size list, I got 87624 for the list and 120 for the generator. Why the difference? Does it have anything to do with 32 bit vs 64 bit systems?

    • @uttamsharma6358
      @uttamsharma6358 3 ปีที่แล้ว

      I got 85176 for the list and 112 for the generator. I wonder why.

    • @DrDeuteron
      @DrDeuteron 2 ปีที่แล้ว

      the list is a list, meanwhile the generator is a function capable of computing the list.

  • @jijie133
    @jijie133 ปีที่แล้ว

    Great video.

  • @TruthSikher
    @TruthSikher 4 ปีที่แล้ว +1

    This isn't really related to generators, but why do you define a separate next method rather than just writing the code inside the dunder next method?

  • @1980legend
    @1980legend 4 ปีที่แล้ว

    What happens if you need to collect all the values? Do you still need the list comprehension, or can you output the generator to a list, and does that save any memory compared to the list comprehension? Thanks

    • @loganrowe1727
      @loganrowe1727 4 ปีที่แล้ว

      @1980legend if you need all of the values at the same time, then you cannot save any memory using a generator. But if for example you only need to remember the last 100 values or the largest 100 values of the values seen so far, then you could feed the generator into a queue or heap and reduce your overall memory usage.

    • @1980legend
      @1980legend 4 ปีที่แล้ว

      @@loganrowe1727 thanks. So just so I have this straight. If you iterate through an array or list but after you need access to all the resulting values, you don’t gain anything by using a generator? If that’s so what’s the point of a generator? To pipe a number through a function in-line for collection later?

    • @loganrowe1727
      @loganrowe1727 4 ปีที่แล้ว

      @@1980legend As soon as you make an array to hold the N values a generator would create, then a generator serves no purpose because O(N) memory would be used. However when you only need one or a few of the N values, then you can use a generator to reduce the total memory.
      i.e.: for i in range(N): range(N) is a generator so instead of storing N values in memory you only need store one value at a time so it is O(1) but for i in list(range(N)): creates a an array that stores N values so it requires O(N) memory.

    • @1980legend
      @1980legend 4 ปีที่แล้ว

      @@loganrowe1727 cool thanks man

  • @learnserve
    @learnserve 11 หลายเดือนก่อน

    Then, I guess, range() also uses yield keyword, right? I used it a lot in C# with many Reflection APIs: the function actually don't run when it is called, but only when the returned value's iterator is requested, just until the next yield keyword at which time it is paused...

  • @ultimatejy9393
    @ultimatejy9393 3 ปีที่แล้ว

    I have the exact code with Tim, I try to get the generator size, and it prints 112, does memory consumption different in machines?

  • @B7HALL21
    @B7HALL21 3 ปีที่แล้ว

    great vid dude

  • @powerfulLVE
    @powerfulLVE 3 ปีที่แล้ว +1

    Thanks!

  • @notafascist3171
    @notafascist3171 4 ปีที่แล้ว +1

    hi tim, i by no means am following this tutorial but i would like to know how to download pygame onto python 3.8 on IDLE.

  • @PragmaticGamer
    @PragmaticGamer 4 ปีที่แล้ว

    What is a good software to edit video in Linux?

  • @tejasbirsingh8503
    @tejasbirsingh8503 4 ปีที่แล้ว

    Tim , have you use python for programming problems and data structures?

  • @blakebiz2335
    @blakebiz2335 4 ปีที่แล้ว +1

    Did you accidentally post this on private I can’t see it on your channel can only get here by link?

    • @TechWithTim
      @TechWithTim  4 ปีที่แล้ว +1

      Nope, think it's just loading still. Literally just uploaded it

  • @pkavenger9990
    @pkavenger9990 4 ปีที่แล้ว

    so let me rephrase it in a very simple way:
    return vs yeild in a function or method be like:
    return: i am doing barbell curls in a gym and also counting my reps, so my focus is on muscles as well on counting.
    yeild: i am doing barbell curls but i ask my friend whos name is "generator" to count for me while i am doing the exercise with a free mind.
    This will consume less memory of our brain practically.

  • @kanchiaaje3616
    @kanchiaaje3616 4 ปีที่แล้ว

    Hello Tim.
    I am learning your pygame tutorial series.I have a question.I downloaded
    the sprite sheet of Itachi and i was able to move him left and right.
    but i wanted to do a power attack just by pressing a key.
    How can i make him to do that animation by loading images .
    Please reply

  • @Shane-ig4pg
    @Shane-ig4pg 4 ปีที่แล้ว

    Heyy Tim! Nice vid,
    (It’s Shane btw)

  • @zelytics
    @zelytics 3 ปีที่แล้ว

    Why not use e notation for a large number?

  • @wongkingshun
    @wongkingshun 2 ปีที่แล้ว

    def gen(n)
    for i in range(n):
    yield i**2
    g=gen(10000)

  • @bigmancozmo
    @bigmancozmo 4 ปีที่แล้ว

    thank you for your python tutorials now i know how to zip a file that the user input

  • @hectorminator4
    @hectorminator4 3 ปีที่แล้ว

    Great video as always, my question is this, is there any way to reset the generator, so you can iterate greater than once an item??

    • @DrDeuteron
      @DrDeuteron 2 ปีที่แล้ว

      no, but you can pre-make a copy with itertools.tee

  • @yogeshpandey1612
    @yogeshpandey1612 4 ปีที่แล้ว

    It helps me a lot thanks

  • @PixelFerret
    @PixelFerret 4 ปีที่แล้ว

    As always, amazing tutorials.
    Looking at the metaprograming tutorial made me question, is it possible to bring the DateTime helpers from ruby onrails to python? you can do very nice things with those, mainly for time calculation like 10.minutes.from_now or 2.hours + 20.minutew + 15.seconds

    • @nmertsch8725
      @nmertsch8725 4 ปีที่แล้ว +1

      You can totally do that, yes. The built-in pathlib library does a similar thing with file paths: Path objects implement __div__ to allow /-chaining of paths.
      Your datetime idea might be a great little project to practice how to use dunder methods in Python :)

  • @iulianmatei7336
    @iulianmatei7336 4 ปีที่แล้ว +2

    Hi Tim

  • @Pilot_engineer_19
    @Pilot_engineer_19 4 ปีที่แล้ว +1

    Kite incompatible with Spyder 3.3

  • @vitalizing7165
    @vitalizing7165 4 ปีที่แล้ว

    Good stuff. Could you do a video on lambdas, and also coroutines?

  • @ddague-kp3ub
    @ddague-kp3ub 3 ปีที่แล้ว

    Thanks a lot

  • @BossNerd
    @BossNerd 3 ปีที่แล้ว

    Does anyone know why I am getting twice the memory usage he gets? I am on a workstation using ECC ram - could that be it?

  • @hannesthiersen8170
    @hannesthiersen8170 4 ปีที่แล้ว +1

    Is extending Python code with C/C++ programs considered expert Python or expert C/C++?

    • @ekrem_dincel
      @ekrem_dincel 4 ปีที่แล้ว

      Nothing, you can copy pasta from net.
      I mean it is all about what you want to do.

  • @revarevanth3847
    @revarevanth3847 4 ปีที่แล้ว

    Please teach some of the most used bulitin fuctions like filter map reduce zip izip nd many more

  • @adrianvorcak1143
    @adrianvorcak1143 4 ปีที่แล้ว

    x1 = np.array(i**2 for i in range(10000))
    print(sys.getsizeof(x1))
    44 bytes

    • @DrDeuteron
      @DrDeuteron 2 ปีที่แล้ว

      that's because you have a length 1 array of generators, not of the generator''s In
      [17]: x1 = array(i**2 for i in range(10))
      In [18]: x1
      Out[18]: array(, dtype=object)
      In [19]: x2 = array([i**2 for i in range(10)])
      In [20]: x2
      Out[20]: array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81])

  • @GhostixMusic
    @GhostixMusic 4 ปีที่แล้ว +1

    I luv u bro. ♥

  • @EpicHardware
    @EpicHardware 3 ปีที่แล้ว

    i was curious if i can fill 96Gb ram, this code did it, but it took ages !

  • @fentonmsu
    @fentonmsu 4 ปีที่แล้ว

    wow, how do you remember all these functions when your are teaching, i don't see any edit outs

  • @freemanfreed1581
    @freemanfreed1581 3 ปีที่แล้ว

    doesn't the class need an __iter__() method ??

  • @PaulvonHoesslin
    @PaulvonHoesslin 3 ปีที่แล้ว

    my brain just exploded.

  • @_whitecatfullgrown
    @_whitecatfullgrown 3 ปีที่แล้ว

    Oh yeah i just learned yield from niel patel books and here comes the explanation, thanks bro i enjoyed the video

  • @erickalvarez6486
    @erickalvarez6486 4 ปีที่แล้ว

    Totally amazing, I´m going through all of your videos bro, you de best. I´d like to know if you know about Cython, it´s off topic but I´d like to do faster programs with Cython and I was wondering if you may know. Anyways, keep your hard work, I´m following.

  • @brianm2572
    @brianm2572 4 ปีที่แล้ว

    nice vid man

  • @gamerstrim
    @gamerstrim 4 ปีที่แล้ว

    I think the most important and the most hard theme it is recursion.

  • @wiloghby
    @wiloghby 4 ปีที่แล้ว +2

    Tim is super cute!

  • @samvillie1000
    @samvillie1000 4 ปีที่แล้ว

    How much time do you spend reading each day?

  • @bahibrahim101
    @bahibrahim101 ปีที่แล้ว

    Ty

  • @sainco3036
    @sainco3036 4 ปีที่แล้ว

    Thanks.

  • @infinityy1466
    @infinityy1466 3 ปีที่แล้ว

    lmaoo watching 1 hour before my exam

  • @kickflipz
    @kickflipz 3 ปีที่แล้ว

    Gaanerator

  • @SkyFly19853
    @SkyFly19853 4 ปีที่แล้ว

    So, generators speed up the process for Python.

    • @TechWithTim
      @TechWithTim  4 ปีที่แล้ว +1

      It’s not necessarily speed, it’s just optimizing memory

    • @SkyFly19853
      @SkyFly19853 4 ปีที่แล้ว

      @@TechWithTim
      I understand.
      That feature is still useful.
      💯💯💯💯💯💯

    • @nmertsch8725
      @nmertsch8725 4 ปีที่แล้ว +1

      I've seen a substantial speed up by using generators over lists, but this really depends on how you implement both approaches. It mostly comes down to memory management overhead that can be reduced by using generators.

    • @SkyFly19853
      @SkyFly19853 4 ปีที่แล้ว

      @@nmertsch8725
      I understand what you mean.

  • @ekrem_dincel
    @ekrem_dincel 4 ปีที่แล้ว

    Bro just use (i**2 for i in range(1000000000))

    • @TechWithTim
      @TechWithTim  4 ปีที่แล้ว +2

      No that’s a generator comprehension

    • @ekrem_dincel
      @ekrem_dincel 4 ปีที่แล้ว +1

      @@TechWithTim good video Tim, show us this next time.

  • @s000sdas
    @s000sdas 4 ปีที่แล้ว

    Too much kite

  • @JakubSK
    @JakubSK ปีที่แล้ว

    If you call it an Expert tutorial, you should have gone through all the other examples like complex computations, closing/stopping, inputs, etc.

    • @jiexifan3573
      @jiexifan3573 5 หลายเดือนก่อน

      🤓👆