Python Tutorial: Generators - How to use them and the benefits you receive

Sdílet
Vložit
  • čas přidán 9. 07. 2024
  • Python Generators are often considered a somewhat advanced topic, but they are actually very easy to understand once you start using them on a regular basis. Actually, after you use generators for some time, you will often find them more readable and performant than other options.
    In this video, we will look at what a python generator is, how and why we would use one, and the performance benefits they give us.
    The code from this video can be found at:
    github.com/CoreyMSchafer/code...
    ✅ Support My Channel Through Patreon:
    / coreyms
    ✅ Become a Channel Member:
    / @coreyms
    ✅ One-Time Contribution Through PayPal:
    goo.gl/649HFY
    ✅ Cryptocurrency Donations:
    Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
    Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
    Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
    ✅ Corey's Public Amazon Wishlist
    a.co/inIyro1
    ✅ Equipment I Use and Books I Recommend:
    www.amazon.com/shop/coreyschafer
    ▶️ You Can Find Me On:
    My Website - coreyms.com/
    My Second Channel - / coreymschafer
    Facebook - / coreymschafer
    Twitter - / coreymschafer
    Instagram - / coreymschafer
    #Python

Komentáře • 664

  • @cooleekova
    @cooleekova Před 2 lety +70

    6 years later your explanation on generators in Python is still the best on CZcams 💙

  • @ocnyoura6803
    @ocnyoura6803 Před 8 lety +447

    @Corey Schafer, I've used Python for half a year now and never really used generator functions and expressions before. Thanks for introducing me to them. You got some of the most coherent, clear and concise Python tutorials out there. And I've seen a lot of them.

  • @TheFourOnSix
    @TheFourOnSix Před 7 lety +603

    I can see this being incredibly important and applicable in data science when working with large data sets!

    • @AlqGo
      @AlqGo Před 6 lety +4

      Another very useful application is to use generators on custom data structures. You can define generators to iterate over your own data structure however you want them to (within the limits of what generators can do).

    • @Neuroszima
      @Neuroszima Před 5 lety +7

      Personally i don't get the attention generators get. It is just an iterator with much more simple syntax than iter class in python, which i preffer more since i can customize it more through data model methods, and custom methods put in class declaration. Unless you use it to generate a number of a sequence, you will always need to hold data in memory. You get 2 advantages od generators then. One is yielding values on demand (which is what iterator already does), and the other is simplified syntax.

    • @user-he4ef9br7z
      @user-he4ef9br7z Před 4 lety +4

      yeah , i needed to use em when dealing with permutations, whivh is very memory intensive otherwise

    • @TheSkepticSkwerl
      @TheSkepticSkwerl Před 3 lety +1

      @@Neuroszima I don't use generators, still learning, but it seems they are beneficial in memory saving, as you can work with gigantic datasets, but only get the data you need. But I feel you could do the same thing with a regular iterator of any sort, if you code it right.

    • @wonkysouceaasdsad9664
      @wonkysouceaasdsad9664 Před 3 lety +1

      i use them a lot for data mining

  • @funkymunky8787
    @funkymunky8787 Před 7 lety +168

    You are actually a great tutor: you always show use cases, explain what goes on under the hood, and have a good pace so it doesn't get boring. Very highly recommended!

    • @saurabhkulkarni2177
      @saurabhkulkarni2177 Před 5 lety +3

      and you funkymunky8787, great at explaining how he explains:)

    • @bharatiagrawal71
      @bharatiagrawal71 Před 3 lety +1

      @@saurabhkulkarni2177 and you saurabh kularni, are great at explaining how funkymunky8787 is great at explaining how he explains :)

  • @RyomaEchizenAnime
    @RyomaEchizenAnime Před 8 lety +14

    You seriously don't know how much you've helped me, thank you.

  • @chunchunmaru2760
    @chunchunmaru2760 Před 5 lety +18

    Literally never heard of generators and I've been doing Python for a while, great video.

  • @joshiravishankar
    @joshiravishankar Před 4 lety +4

    Not only about this video, but about any & all of your videos, they are to the point and designed to grasp at a steady pace. Doesn't overflow at any time. I come here time to time to revisit the concepts when I can't recall anything, because you have explained them so well that, we can understand them from scratch in no time. Best python tutorials on the net and better than any paid one too. Thank you again :)

  • @nriezedichisom1676
    @nriezedichisom1676 Před 2 lety

    I have gone through this topic many times from other sources and this is the first and only time it made sense. Thank you so much for taking out the time to make this.

  • @MrGaffeltand
    @MrGaffeltand Před 5 lety +3

    I instantly understand the function of generators.
    Thank you very much, great explaining!

  • @carolynsaund5111
    @carolynsaund5111 Před 3 lety +10

    I never knew I needed Matthew McConaughey to teach me how to use generator objects but I did. Thanks pal, great vid.

  • @BabakKeyvani0
    @BabakKeyvani0 Před 7 lety +1

    One of the best videos in your python series :) Thanks for the concise and helpful performance comparisons :).

  • @kevinfreeman1101
    @kevinfreeman1101 Před 5 lety +3

    @Corey: You present these in a clear, understandable fashion with plenty of proof to demonstrate the huge performance boost that generators make possible. This really helps these concepts to sink in, and helps me to understand how I can implement this concept into my own code. This is awesome, thanks!

  • @rahulrs6996
    @rahulrs6996 Před 5 lety +1

    This was very very helpful, was going through other tutorials about generators and none of em actually helped me understand this concept easier.. thanks a lot Corey... :)

  • @ACLAproductions
    @ACLAproductions Před 8 lety +10

    Thanks for the video! You made it very easy to understand :)

  • @TrickstaleX
    @TrickstaleX Před 4 lety +7

    Thank you so much for your videos! Working in my master's thesis with python and huge data sets and I think I can really put this all into use!

    • @oxey_
      @oxey_ Před 3 lety

      How'd it go?

  • @arnavsingh0
    @arnavsingh0 Před 5 lety +2

    I have watched a hell of tutorials but man, you, you are just awesome. Hats off to you.

  • @caleba.robinson8451
    @caleba.robinson8451 Před 3 lety

    Awesome video! I really appreciate that I not only know how to use generators now, but also have some ideas for use-cases because of the advantages you explained.

  •  Před 4 lety +1

    I, finally, understood the advantages of Python, thanks you all guys who post this videos

  • @nasenblick
    @nasenblick Před rokem

    By far the easiest to understand tutorial on generators I‘ve found so far. This is great- thanks much!

  • @heyrmi
    @heyrmi Před 3 lety +1

    even after 5 years, this is so relevant and clear. thanks!

  • @Platipus23
    @Platipus23 Před 4 lety +7

    I can't even put into words how much I love this channel.

  • @danielcrigan1301
    @danielcrigan1301 Před 5 měsíci

    Every topic I cover on Python with the other resources, I have to come back to your videos and solidify my knowledge on that! Your videos are great, clear, short and straight to the point! I love it

  • @veerannabandaru1122
    @veerannabandaru1122 Před 4 lety +1

    @Corey Schafer, I have seen many videos and read many articles on generators and iterators but this video gave me better understanding. Thanks for the video.

  • @vamsikrishnareddicherla6576

    Crystal Clear and to the point explanation as always.

  • @plmacleod
    @plmacleod Před 2 lety +10

    Thank you, Corey. This (along with data structure) is a game-changer for me in terms of efficiency. I'm surprised why most Python courses don't teach this.

  • @olehigorovich474
    @olehigorovich474 Před 4 lety +1

    Priceless tutorial. Thank you, Corey.

  • @Pangpizza
    @Pangpizza Před 7 měsíci

    I just want to say that you Corey blow my mind when it comes to easily understandable explanations. It is so clear, to-the-point, relevant and useful. Keep it up! Donated to you now!

  • @hobby_coding
    @hobby_coding Před 4 lety

    i didn't check all python tutorial in youtube yet but i think this is best one - please keep the good work so many ppl are learning from you -

  • @soumyazyx
    @soumyazyx Před 4 lety +2

    Thank you Corey for your time and effort. Keep spreading the knowledge.

  • @Praveshan0710
    @Praveshan0710 Před měsícem

    That memory usage example at the end was perfect, thank you! It really helped me understand this concept.

  • @SapnaSaini3
    @SapnaSaini3 Před 7 lety +1

    Your all python tutorials are amazing ...

  • @Kralnor
    @Kralnor Před 4 lety +1

    Excellent explanation of generators. I've been using Python on and off for a while, but I never looked into them. Your example where you used list comprehension as a generator expression was particularly insightful.

  • @noobsdaestrategia
    @noobsdaestrategia Před 5 lety +2

    Straight to the point, amazing!

  • @codegeek7832
    @codegeek7832 Před 8 lety +18

    hey there Corey you are definitely the man, any follow up tuts on django framework many i have found online are just a bare example of simple apps nothing more about framework itself ,i like very much your way of teaching you are great tutor i hope will see soon some django tutorials thank you for your work

  • @RameenFallschirmjager
    @RameenFallschirmjager Před 3 lety

    Best explanation ever! You explain things beautifully and elegantly! The quality of this channel is superb and I learnt and learning a lot from you sir!

  • @nikolahuang1919
    @nikolahuang1919 Před 6 lety

    his courses are always so clear and helpful.

  • @Leonardo-jv1ls
    @Leonardo-jv1ls Před 4 lety +2

    Man. You are incredible good in explaining. Congratulations.

  • @firehelp6040
    @firehelp6040 Před 8 lety +1

    Thanks for much for this. Read stackoverflow for hours but couldn't get it. Your video was v clear and I finally understand it.

  • @christianbaier1190
    @christianbaier1190 Před 5 lety +1

    Well structured. Well articulated. Corey Schafer.

  • @HSWang-ym2mm
    @HSWang-ym2mm Před 7 lety

    Hi, after watching several your videos, I figure out they are really helpful for me. Thanks so much for sharing your knowledge!

  • @khaledadrani3184
    @khaledadrani3184 Před 3 lety

    Thank you, a very clear and helpful tutorial! I finally got a grasp on the generators and yield stuff!

  • @jonoisedev
    @jonoisedev Před 3 lety +2

    I really enjoy spending my nights learning Python with Corey!

  • @subhadeepkaran1568
    @subhadeepkaran1568 Před 7 lety +2

    Subscribed to your channel, sir. That explanation was pretty neat and right to the meat. Thanks a lot.

  • @jaydave29990
    @jaydave29990 Před 4 lety

    Excellent example and explanation, Corey. Loved it!

  • @bensacc
    @bensacc Před 8 lety

    these videos really are outstanding. thank you so much for doing them!

  • @zbigniewbrzezinski8869
    @zbigniewbrzezinski8869 Před 4 lety +1

    Thank you for this
    short and instructive video.

  • @TommyLate
    @TommyLate Před 6 lety +2

    Right now helped me even better than the video from the udacity course im doin right now or to say it in another way: it was a really worthy addition! Thx :)

  • @skannanji
    @skannanji Před 2 lety

    One of the best video I have seen in youtube respective of generator.. Thank you man.

  • @adam.m.rosman
    @adam.m.rosman Před 10 měsíci

    so great! corey is such a python wizard he doesn't even have to wrap his print statements in parentheses.

  • @livedlife5930
    @livedlife5930 Před 5 lety +2

    Thanks a lot for detail explanation with meaningful example.

  • @slagunes4705
    @slagunes4705 Před 5 lety +15

    I read the same topic once in my native language and I didn't get it. XD
    But your tutorial explained it clearly, thank you. :)

  • @amatya.rakshasa
    @amatya.rakshasa Před 3 lety

    Man.. you're an EXCELLENT teacher. Thank you!

  • @sudarsandm
    @sudarsandm Před 2 lety

    And also you practically demonstrated how generators would give great savings on time factor (execution) and memory. You are the best.

  • @IPv6Freely
    @IPv6Freely Před 5 lety

    Great video. Made the entire thing easy to understand. Thanks!

  • @Edgar-th1zk
    @Edgar-th1zk Před 8 lety

    This video was great. Thank you for taking the time.

  • @BossLikesShenanigans
    @BossLikesShenanigans Před 8 lety

    This was a really great video. Awesome job man.

  • @sudarsandm
    @sudarsandm Před 2 lety

    Because of you Corey I was able to get offers for a contract job at Cisco and Google as Python test Automation Engineer. Thank you very much.

  • @vaibhavkhobragade9773
    @vaibhavkhobragade9773 Před 6 lety +1

    Thanks bro! such nice explanation and such great voice!

  • @mahmoudelzar213
    @mahmoudelzar213 Před 5 lety +1

    Easy to understand, thank you.

  • @hanshima_
    @hanshima_ Před 2 měsíci

    Amazing, thanks for the content. I really needed to understand this to solve a memory problem and I didn't know it would be that easy.

  • @catatonico123
    @catatonico123 Před 8 lety

    Great explanations. Plus, it's nice to see this kind of intermediate subjects treated, I guess this is a space that needs to be filled.

  • @zayeba
    @zayeba Před 5 lety +3

    Damn, he explained it so clear and easy to digest.

  • @egehurturk6209
    @egehurturk6209 Před 4 lety +1

    Wow, amazing video and amazing introduction to generators. Thank you!

  • @badoiuecristian
    @badoiuecristian Před 5 lety +7

    In a nutshell I can see the benefit of saving memory, dealing with large data sets that would not fit into memory. As for processing power it will be the same when you have to loop trough the generator. Very well explained.

    • @nonconsensualopinion
      @nonconsensualopinion Před 4 lety +1

      Looping through still results in the generator being superior. I tried it on my system and it is faster still. I suspect it's because not using all that memory at once reduces pressure on the system cache.

  • @haoyangrocks
    @haoyangrocks Před 6 lety

    Thank you! You explained things very clearly!

  • @dirtypeach
    @dirtypeach Před rokem

    Great explanation, and even greater use-case break down. Thanks.

  • @srinivasreddydanda1777

    Perfect explanation with time and space consumption. Thanks!!

  • @dp0813
    @dp0813 Před 5 lety +1

    Awesome vid as always!

  • @parkerbelholland1037
    @parkerbelholland1037 Před 4 lety

    Old vid, but I JUST started Python 3 about 6 months ago and had never even heard of generators, so thanks for continuing my Python journey. I have plenty of coding experience, so I'd love to know what other esoteric info/concepts are worth learning in the esoteric Python world.

  • @bernhard0815
    @bernhard0815 Před 4 lety +1

    HOLY MOLY! Thanks for this helpful content!

  • @brenlyd
    @brenlyd Před 6 lety

    This lecture was great! You are truely a stand up guy

  • @sohamajgaonkar3119
    @sohamajgaonkar3119 Před 3 lety

    awesome dude..... short and complete. u made it super simple. u just got a subscriber.

  • @svikas641
    @svikas641 Před 4 lety +3

    The music at 11:10 reminds me of Ross playing keyboard lol anyways great piece of work man. Truly appreciate it.

  • @koreanbroadcastarchive306

    This is incredibly helpful. Thank you!

  • @434neta
    @434neta Před 2 lety

    Awesome content, explained in an easy and clear way. Very clear English. Thank you!!

  • @stillinmusic
    @stillinmusic Před 6 lety +1

    Brilliant explanation!

  • @raminrad666
    @raminrad666 Před 4 lety

    You are a brilliant teacher and I thank you sir.

  • @markkupaaso9642
    @markkupaaso9642 Před 6 lety +4

    Hi, Corey! Thanks for the exceptional tutorials that you keep generating (pun intended).
    I'd like to kindly put forth a proposition that you'd make a tutorial on the async -feature of Python language.
    The subject needs the clarity of your expression. Can't wait to next towards that.

  • @sampadai972
    @sampadai972 Před 7 lety

    Thank you so much, very neatly explained. Helping me alot

  • @Geekyology
    @Geekyology Před 6 lety +1

    Exceptionally nice tutorials :)

  • @YeekyYeeky
    @YeekyYeeky Před 4 lety +1

    thank you very much , your explanation is really clear

  • @dufreebell
    @dufreebell Před 3 lety

    First time watching your video. Very useful 👏🏼. Can’t resist from clicking that subscribe button. Well done.

  • @joaovictorpereirarocha33

    I never understood why I should use generator but your example was perfect to understand. Thanks!

  • @mertyigit4093
    @mertyigit4093 Před 2 lety

    Most clear and neat explanation! Respect

  • @gtsmeg3474
    @gtsmeg3474 Před 5 lety +1

    Thanks for sharing knowledge properly

  • @uppubhai
    @uppubhai Před 7 lety

    WOW after finaaly using python for a year i am getting generators. thanks a lot sir

  • @Circumvenscion
    @Circumvenscion Před 4 lety +1

    Excellent video. Well explained and communicated.

  • @imherovirat
    @imherovirat Před 4 lety +1

    This is awesome. I generally don't comment but this video is whole next level. Before watching this video i had a lot of quires in my mind about the generators. But now I think i am clear about it. Thanks Corey for this wonderful explanation. Probably the best explanation till now !!!
    Love from Nepal ♥

  • @ilanaizelman3993
    @ilanaizelman3993 Před 5 lety +1

    GOLD.
    Thank you!

  • @Grinwa
    @Grinwa Před rokem

    Oh my goodness thank you so much 💖
    Just what I needed i started working on a small project and am a beginner in python

  • @writtiknath2226
    @writtiknath2226 Před 3 lety

    Very important. Thank you, well explained 🔥

  • @chtulurr
    @chtulurr Před 2 lety

    Such a fantastic demonstration

  • @vimalraj7777
    @vimalraj7777 Před 5 lety +1

    Simply awesome tutorial

  • @theartofmountaingoating184

    Your explanation was clear and on point. Thank you sir

  • @user-hn7qh9tw8g
    @user-hn7qh9tw8g Před rokem

    Thank you so much corey sir for this example. lots of love.

  • @santhoshckumar7367
    @santhoshckumar7367 Před 3 lety

    A great tutor indeed. 🙏🙏🙏

  • @abhishek-shrm
    @abhishek-shrm Před 2 lety +1

    Even after 5 years, it's the best explanation :)

  • @pawanbhandarkar4199
    @pawanbhandarkar4199 Před 6 lety +1

    Excellent! Thanks so much :D

  • @yoyocr2034
    @yoyocr2034 Před 3 lety

    hands down this is the best python channel in youtube

  • @mayurarora6795
    @mayurarora6795 Před 5 lety +1

    great explanation in very easy way.