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

Поделиться
HTML-код
  • Опубликовано: 22 май 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

Комментарии • 661

  • @cooleekova
    @cooleekova Год назад +63

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

  • @ocnyoura6803
    @ocnyoura6803 8 лет назад +444

    @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.

    • @coreyms
      @coreyms  8 лет назад +56

      +OCN YOURA6 Thanks! I appreciate the kind words.

    • @Roman-jf8ne
      @Roman-jf8ne 7 лет назад +1

      Awesome!

    • @simon5771
      @simon5771 5 лет назад +1

      I agree.

  • @TheFourOnSix
    @TheFourOnSix 7 лет назад +600

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

    • @AlqGo
      @AlqGo 6 лет назад +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 5 лет назад +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 4 года назад +4

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

    • @TheSkepticSkwerl
      @TheSkepticSkwerl 3 года назад +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 3 года назад +1

      i use them a lot for data mining

  • @funkymunky8787
    @funkymunky8787 7 лет назад +166

    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 5 лет назад +3

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

    • @bharatiagrawal71
      @bharatiagrawal71 3 года назад +1

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

  • @joshiravishankar
    @joshiravishankar 4 года назад +3

    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 :)

  • @chunchunmaru2760
    @chunchunmaru2760 4 года назад +18

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

  • @nriezedichisom1676
    @nriezedichisom1676 Год назад

    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.

  • @RyomaEchizenAnime
    @RyomaEchizenAnime 8 лет назад +14

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

  • @Praveshan0710
    @Praveshan0710 День назад

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

  • @kevinfreeman1101
    @kevinfreeman1101 5 лет назад +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!

  • @carolynsaund5111
    @carolynsaund5111 3 года назад +10

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

  • @plmacleod
    @plmacleod Год назад +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.

  • @danielcrigan1301
    @danielcrigan1301 3 месяца назад

    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

  • @Kralnor
    @Kralnor 3 года назад +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.

  • @rahulrs6996
    @rahulrs6996 5 лет назад +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... :)

  • @Pangpizza
    @Pangpizza 6 месяцев назад

    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!

  • @nasenblick
    @nasenblick Год назад

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

  • @caleba.robinson8451
    @caleba.robinson8451 3 года назад

    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.

  • @MrGaffeltand
    @MrGaffeltand 5 лет назад +3

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

  • @TrickstaleX
    @TrickstaleX 4 года назад +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_ 3 года назад

      How'd it go?

  • @veerannabandaru1122
    @veerannabandaru1122 4 года назад +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.

  • @hobby_coding
    @hobby_coding 3 года назад

    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 -

  • @arnavsingh0
    @arnavsingh0 5 лет назад +2

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

  • @codegeek7832
    @codegeek7832 7 лет назад +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

  • @parkerbelholland1037
    @parkerbelholland1037 4 года назад

    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.

  • @firehelp6040
    @firehelp6040 8 лет назад +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.

  •  4 года назад +1

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

  • @RameenFallschirmjager
    @RameenFallschirmjager 3 года назад

    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!

  • @BabakKeyvani0
    @BabakKeyvani0 7 лет назад +1

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

  • @sudarsandm
    @sudarsandm 2 года назад

    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.

  • @slagunes4705
    @slagunes4705 5 лет назад +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. :)

  • @Platipus23
    @Platipus23 4 года назад +7

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

  • @heyrmi
    @heyrmi 3 года назад +1

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

  • @HSWang-ym2mm
    @HSWang-ym2mm 6 лет назад

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

  • @sudarsandm
    @sudarsandm 2 года назад

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

  • @hanshima_
    @hanshima_ Месяц назад

    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.

  • @vamsikrishnareddicherla6576
    @vamsikrishnareddicherla6576 5 лет назад +1

    Crystal Clear and to the point explanation as always.

  • @joaovictorpereirarocha33
    @joaovictorpereirarocha33 3 года назад

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

  • @ACLAproductions
    @ACLAproductions 8 лет назад +10

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

  • @skannanji
    @skannanji 2 года назад

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

  • @Leonardo-jv1ls
    @Leonardo-jv1ls 4 года назад +2

    Man. You are incredible good in explaining. Congratulations.

  • @SapnaSaini3
    @SapnaSaini3 7 лет назад +1

    Your all python tutorials are amazing ...

  • @markkupaaso9642
    @markkupaaso9642 6 лет назад +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.

  • @subhadeepkaran1568
    @subhadeepkaran1568 7 лет назад +2

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

  • @adam.m.rosman
    @adam.m.rosman 8 месяцев назад

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

  • @badoiuecristian
    @badoiuecristian 5 лет назад +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 4 года назад +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.

  • @TommyLate
    @TommyLate 5 лет назад +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 :)

  • @jonoisedev
    @jonoisedev 3 года назад +2

    I really enjoy spending my nights learning Python with Corey!

  • @nikolahuang1919
    @nikolahuang1919 6 лет назад

    his courses are always so clear and helpful.

  • @olehigorovich474
    @olehigorovich474 4 года назад +1

    Priceless tutorial. Thank you, Corey.

  • @soumyazyx
    @soumyazyx 4 года назад +2

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

  • @khaledadrani3184
    @khaledadrani3184 3 года назад

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

  • @imherovirat
    @imherovirat 4 года назад +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 ♥

  • @christianbaier1190
    @christianbaier1190 5 лет назад +1

    Well structured. Well articulated. Corey Schafer.

  • @sohamajgaonkar3119
    @sohamajgaonkar3119 3 года назад

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

  • @sungchulyonseiackr
    @sungchulyonseiackr 5 лет назад

    I always enjoy watching your video. Great work!!!
    Two Comments on your outdated code that I passed through:
    1. Instead of time.clock(), time.time() is recommended according to my web surfing.
    2. Your memory_usage_psutil() function seems outdated. I did some web search and I found one solution from stackoverflow. Unfortunately I lost ulr since I had to shut down my jupyter notebook. But here is what I found essentially:
    import os
    import psutil
    def memory_usage_psutil():
    pid = os.getpid()
    py = psutil.Process(pid)
    return py.memory_info()[0]/2.**30 # memory use in GB
    This works.

  • @jaydave29990
    @jaydave29990 4 года назад

    Excellent example and explanation, Corey. Loved it!

  • @amatya.rakshasa
    @amatya.rakshasa 3 года назад

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

  • @uppubhai
    @uppubhai 6 лет назад

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

  • @Vijay-Yarramsetty
    @Vijay-Yarramsetty 3 месяца назад

    who else got here to brush-up their knowledge.
    You are one of the youtubers I relied heavily to get a developer job. Thanks Corey!

  • @catatonico123
    @catatonico123 8 лет назад

    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.

  • @srinivasreddydanda1777
    @srinivasreddydanda1777 3 года назад

    Perfect explanation with time and space consumption. Thanks!!

  • @yoyocr2034
    @yoyocr2034 3 года назад

    hands down this is the best python channel in youtube

  • @Grinwa
    @Grinwa Год назад

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

  • @zayeba
    @zayeba 5 лет назад +3

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

  • @svikas641
    @svikas641 4 года назад +3

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

  • @livedlife5930
    @livedlife5930 5 лет назад +2

    Thanks a lot for detail explanation with meaningful example.

  • @434neta
    @434neta 2 года назад

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

  • @dufreebell
    @dufreebell 3 года назад

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

  • @zbigniewbrzezinski8869
    @zbigniewbrzezinski8869 4 года назад +1

    Thank you for this
    short and instructive video.

  • @noobsdaestrategia
    @noobsdaestrategia 5 лет назад +2

    Straight to the point, amazing!

  • @brenlyd
    @brenlyd 6 лет назад

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

  • @vaibhavkhobragade9773
    @vaibhavkhobragade9773 6 лет назад +1

    Thanks bro! such nice explanation and such great voice!

  • @IPv6Freely
    @IPv6Freely 5 лет назад

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

  • @indrajithlal6160
    @indrajithlal6160 3 года назад

    this was extremely usefull for webscraping as well. so the prgm i made initially would scrape through the site and store all the results in a list and only after getting all the results and returning it i would be able to display it all at once. and as you could imagine this takes a lot of time but with this , i could easily display each result just as the prgm finds it. so thks a lot

  • @egehurturk6209
    @egehurturk6209 4 года назад +1

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

  • @mertyigit4093
    @mertyigit4093 2 года назад

    Most clear and neat explanation! Respect

  • @theartofmountaingoating184
    @theartofmountaingoating184 3 года назад

    Your explanation was clear and on point. Thank you sir

  • @haoyangrocks
    @haoyangrocks 6 лет назад

    Thank you! You explained things very clearly!

  • @dirtypeach
    @dirtypeach Год назад

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

  • @bensacc
    @bensacc 8 лет назад

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

  • @raminrad666
    @raminrad666 4 года назад

    You are a brilliant teacher and I thank you sir.

  • @mahmoudelzar213
    @mahmoudelzar213 5 лет назад +1

    Easy to understand, thank you.

  • @gtsmeg3474
    @gtsmeg3474 5 лет назад +1

    Thanks for sharing knowledge properly

  • @Circumvenscion
    @Circumvenscion 4 года назад +1

    Excellent video. Well explained and communicated.

  • @user-hn7qh9tw8g
    @user-hn7qh9tw8g Год назад

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

  • @Edgar-th1zk
    @Edgar-th1zk 8 лет назад

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

  • @abhishek-shrm
    @abhishek-shrm 2 года назад +1

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

  • @xmuta
    @xmuta 8 лет назад

    Great video. Watching your Python videos really helped me get a new job as a Python programmer. Thank you.

    • @coreyms
      @coreyms  8 лет назад +1

      +Scott L That's great, Scott. Glad to hear!

  • @stillinmusic
    @stillinmusic 6 лет назад +1

    Brilliant explanation!

  • @howardroth7524
    @howardroth7524 4 года назад +1

    I can see that if you have a large list that causes your O/S to page then using the generator instead would be valuable in terms of optimizing processing speed. My take is that this is something needed for big data. Anyhow, nice well thought out demonstration and it makes me want to explore the use of generators in python, which I have never used before.

  • @BossLikesShenanigans
    @BossLikesShenanigans 8 лет назад

    This was a really great video. Awesome job man.

  • @mayurarora6795
    @mayurarora6795 5 лет назад +1

    great explanation in very easy way.

  • @koreanbroadcastarchive306
    @koreanbroadcastarchive306 3 года назад

    This is incredibly helpful. Thank you!

  • @bernhard0815
    @bernhard0815 4 года назад +1

    HOLY MOLY! Thanks for this helpful content!

  • @saikumartadi1481
    @saikumartadi1481 9 месяцев назад

    Hi sir ! I really found your tutorials very very useful . They are gems . I wish we can see more courses from you. God bless you

  • @chtulurr
    @chtulurr 2 года назад

    Such a fantastic demonstration

  • @writtiknath2226
    @writtiknath2226 3 года назад

    Very important. Thank you, well explained 🔥

  • @sampadai972
    @sampadai972 7 лет назад

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

  • @zealot4325
    @zealot4325 2 года назад

    You are very good in explaining things