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

  Рет қаралды 847,197

Corey Schafer

Corey Schafer

Күн бұрын

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

Пікірлер: 660
@cooleekova
@cooleekova Жыл бұрын
6 years later your explanation on generators in Python is still the best on KZfaq 💙
@rijuroy7415
@rijuroy7415 9 ай бұрын
Exactly
@guilhermeparreira5448
@guilhermeparreira5448 6 ай бұрын
8 years ago right now and it still very well explained hehe
@ocnyoura6803
@ocnyoura6803 8 жыл бұрын
@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 жыл бұрын
+OCN YOURA6 Thanks! I appreciate the kind words.
@Roman-jf8ne
@Roman-jf8ne 7 жыл бұрын
Awesome!
@simon5771
@simon5771 5 жыл бұрын
I agree.
@TheFourOnSix
@TheFourOnSix 7 жыл бұрын
I can see this being incredibly important and applicable in data science when working with large data sets!
@AlqGo
@AlqGo 6 жыл бұрын
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 жыл бұрын
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 жыл бұрын
yeah , i needed to use em when dealing with permutations, whivh is very memory intensive otherwise
@TheSkepticSkwerl
@TheSkepticSkwerl 3 жыл бұрын
@@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 жыл бұрын
i use them a lot for data mining
@funkymunky8787
@funkymunky8787 7 жыл бұрын
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 жыл бұрын
and you funkymunky8787, great at explaining how he explains:)
@bharatiagrawal71
@bharatiagrawal71 3 жыл бұрын
@@saurabhkulkarni2177 and you saurabh kularni, are great at explaining how funkymunky8787 is great at explaining how he explains :)
@RyomaEchizenAnime
@RyomaEchizenAnime 8 жыл бұрын
You seriously don't know how much you've helped me, thank you.
@chunchunmaru2760
@chunchunmaru2760 4 жыл бұрын
Literally never heard of generators and I've been doing Python for a while, great video.
@joshiravishankar
@joshiravishankar 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 Жыл бұрын
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 5 жыл бұрын
I instantly understand the function of generators. Thank you very much, great explaining!
@kevinfreeman1101
@kevinfreeman1101 5 жыл бұрын
@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!
@plmacleod
@plmacleod Жыл бұрын
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.
@rahulrs6996
@rahulrs6996 5 жыл бұрын
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... :)
@carolynsaund5111
@carolynsaund5111 3 жыл бұрын
I never knew I needed Matthew McConaughey to teach me how to use generator objects but I did. Thanks pal, great vid.
@arnavsingh0
@arnavsingh0 5 жыл бұрын
I have watched a hell of tutorials but man, you, you are just awesome. Hats off to you.
@Kralnor
@Kralnor 3 жыл бұрын
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.
@TrickstaleX
@TrickstaleX 4 жыл бұрын
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?
@Platipus23
@Platipus23 4 жыл бұрын
I can't even put into words how much I love this channel.
@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.
@nasenblick
@nasenblick Жыл бұрын
By far the easiest to understand tutorial on generators I‘ve found so far. This is great- thanks much!
@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
@Pangpizza
@Pangpizza 5 ай бұрын
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!
@BabakKeyvani0
@BabakKeyvani0 7 жыл бұрын
One of the best videos in your python series :) Thanks for the concise and helpful performance comparisons :).
@slagunes4705
@slagunes4705 5 жыл бұрын
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. :)
@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.
@veerannabandaru1122
@veerannabandaru1122 4 жыл бұрын
@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.
@codegeek7832
@codegeek7832 7 жыл бұрын
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
@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 -
@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!
@firehelp6040
@firehelp6040 8 жыл бұрын
Thanks for much for this. Read stackoverflow for hours but couldn't get it. Your video was v clear and I finally understand it.
@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!
@Leonardo-jv1ls
@Leonardo-jv1ls 4 жыл бұрын
Man. You are incredible good in explaining. Congratulations.
@vamsikrishnareddicherla6576
@vamsikrishnareddicherla6576 5 жыл бұрын
Crystal Clear and to the point explanation as always.
@heyrmi
@heyrmi 3 жыл бұрын
even after 5 years, this is so relevant and clear. thanks!
@soumyazyx
@soumyazyx 4 жыл бұрын
Thank you Corey for your time and effort. Keep spreading the 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.
@ACLAproductions
@ACLAproductions 8 жыл бұрын
Thanks for the video! You made it very easy to understand :)
@olehigorovich474
@olehigorovich474 4 жыл бұрын
Priceless tutorial. Thank you, Corey.
@skannanji
@skannanji 2 жыл бұрын
One of the best video I have seen in youtube respective of generator.. Thank you man.
4 жыл бұрын
I, finally, understood the advantages of Python, thanks you all guys who post this videos
@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!
@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.
@markkupaaso9642
@markkupaaso9642 6 жыл бұрын
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.
@khaledadrani3184
@khaledadrani3184 3 жыл бұрын
Thank you, a very clear and helpful tutorial! I finally got a grasp on the generators and yield stuff!
@SapnaSaini3
@SapnaSaini3 7 жыл бұрын
Your all python tutorials are amazing ...
@badoiuecristian
@badoiuecristian 5 жыл бұрын
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 жыл бұрын
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.
@jonoisedev
@jonoisedev 3 жыл бұрын
I really enjoy spending my nights learning Python with Corey!
@TommyLate
@TommyLate 5 жыл бұрын
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 :)
@livedlife5930
@livedlife5930 5 жыл бұрын
Thanks a lot for detail explanation with meaningful example.
@amatya.rakshasa
@amatya.rakshasa 3 жыл бұрын
Man.. you're an EXCELLENT teacher. Thank you!
@subhadeepkaran1568
@subhadeepkaran1568 7 жыл бұрын
Subscribed to your channel, sir. That explanation was pretty neat and right to the meat. Thanks a lot.
@imherovirat
@imherovirat 4 жыл бұрын
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 ♥
@joaovictorpereirarocha33
@joaovictorpereirarocha33 2 жыл бұрын
I never understood why I should use generator but your example was perfect to understand. Thanks!
@hanshima_
@hanshima_ 24 күн бұрын
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.
@christianbaier1190
@christianbaier1190 5 жыл бұрын
Well structured. Well articulated. Corey Schafer.
@yoyocr2034
@yoyocr2034 3 жыл бұрын
hands down this is the best python channel in youtube
@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.
@nikolahuang1919
@nikolahuang1919 6 жыл бұрын
his courses are always so clear and helpful.
@srinivasreddydanda1777
@srinivasreddydanda1777 3 жыл бұрын
Perfect explanation with time and space consumption. Thanks!!
@zbigniewbrzezinski8869
@zbigniewbrzezinski8869 4 жыл бұрын
Thank you for this short and instructive video.
@uppubhai
@uppubhai 6 жыл бұрын
WOW after finaaly using python for a year i am getting generators. thanks a lot sir
@saiakhil4751
@saiakhil4751 3 жыл бұрын
I've been searching for reliable sources to learn python. I came across Corey Schafer and never left since then. Corey schafer stan
@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.
@vaibhavkhobragade9773
@vaibhavkhobragade9773 6 жыл бұрын
Thanks bro! such nice explanation and such great voice!
@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
@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.
@jaydave29990
@jaydave29990 4 жыл бұрын
Excellent example and explanation, Corey. Loved it!
@bensacc
@bensacc 8 жыл бұрын
these videos really are outstanding. thank you so much for doing them!
@zayeba
@zayeba 5 жыл бұрын
Damn, he explained it so clear and easy to digest.
@noobsdaestrategia
@noobsdaestrategia 5 жыл бұрын
Straight to the point, amazing!
@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
@dunuth
@dunuth 3 жыл бұрын
5 years later, this is still one of the best tutorials around
@BrianOSheaPlus
@BrianOSheaPlus 3 жыл бұрын
@Raghavan Vidhyasagar in Python version 2 you didn't need to call print with parentheses; print was more like an operator than a function. In Python 3 print is just a regular function, so you need to call it with parentheses.
@sohamajgaonkar3119
@sohamajgaonkar3119 3 жыл бұрын
awesome dude..... short and complete. u made it super simple. u just got a subscriber.
@raminrad666
@raminrad666 4 жыл бұрын
You are a brilliant teacher and I thank you sir.
@haoyangrocks
@haoyangrocks 6 жыл бұрын
Thank you! You explained things very clearly!
@Edgar-th1zk
@Edgar-th1zk 8 жыл бұрын
This video was great. Thank you for taking the time.
@bernhard0815
@bernhard0815 4 жыл бұрын
HOLY MOLY! Thanks for this helpful content!
@gtsmeg3474
@gtsmeg3474 5 жыл бұрын
Thanks for sharing knowledge properly
@brenlyd
@brenlyd 6 жыл бұрын
This lecture was great! You are truely a stand up guy
@abhishek-shrm
@abhishek-shrm 2 жыл бұрын
Even after 5 years, it's the best explanation :)
@BossLikesShenanigans
@BossLikesShenanigans 8 жыл бұрын
This was a really great video. Awesome job man.
@wishurdead444
@wishurdead444 4 жыл бұрын
He knows how to teach. Keep up the good work
@user-hn7qh9tw8g
@user-hn7qh9tw8g Жыл бұрын
Thank you so much corey sir for this example. lots of love.
@ibrahimal-mahfooz8737
@ibrahimal-mahfooz8737 4 жыл бұрын
I will make a generator to thank you for this tutorial. You are the best.
@egehurturk6209
@egehurturk6209 4 жыл бұрын
Wow, amazing video and amazing introduction to generators. Thank you!
@ilanaizelman3993
@ilanaizelman3993 5 жыл бұрын
GOLD. Thank you!
@writtiknath2226
@writtiknath2226 3 жыл бұрын
Very important. Thank you, well explained 🔥
@dufreebell
@dufreebell 3 жыл бұрын
First time watching your video. Very useful 👏🏼. Can’t resist from clicking that subscribe button. Well done.
@svikas641
@svikas641 4 жыл бұрын
The music at 11:10 reminds me of Ross playing keyboard lol anyways great piece of work man. Truly appreciate it.
@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 жыл бұрын
+Scott L That's great, Scott. Glad to hear!
@gabbiparsons438
@gabbiparsons438 3 жыл бұрын
I really appreciate both the way you explain things and your slight Southern accent. It makes it a lot easier for a Georgia girl to understand. Thank you!
@koreanbroadcastarchive306
@koreanbroadcastarchive306 3 жыл бұрын
This is incredibly helpful. Thank you!
@zai_kun
@zai_kun 9 ай бұрын
man.... you are the best
@434neta
@434neta 2 жыл бұрын
Awesome content, explained in an easy and clear way. Very clear English. Thank you!!
@chinivarbasu1599
@chinivarbasu1599 7 жыл бұрын
Thanks for the explanation. it was very helpful. I appreciate the effort buddy
@mertyigit4093
@mertyigit4093 2 жыл бұрын
Most clear and neat explanation! Respect
@theartofmountaingoating184
@theartofmountaingoating184 3 жыл бұрын
Your explanation was clear and on point. Thank you sir
@hikmathamraz7141
@hikmathamraz7141 4 жыл бұрын
your explanation is amazing thank you so much
@stillinmusic
@stillinmusic 6 жыл бұрын
Brilliant explanation!
@mahmoudelzar213
@mahmoudelzar213 5 жыл бұрын
Easy to understand, thank you.
@sampadai972
@sampadai972 7 жыл бұрын
Thank you so much, very neatly explained. Helping me alot
Python Generators Explained
28:37
Tech With Tim
Рет қаралды 141 М.
didn't want to let me in #tiktok
00:20
Анастасия Тарасова
Рет қаралды 11 МЛН
[Vowel]물고기는 물에서 살아야 해🐟🤣Fish have to live in the water #funny
00:53
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 233 М.
Python Generators
15:32
mCoding
Рет қаралды 127 М.
Programming Terms: Closures - How to Use Them and Why They Are Useful
11:44
PLEASE Use These 5 Python Decorators
20:12
Tech With Tim
Рет қаралды 84 М.
Python Tutorial: if __name__ == '__main__'
8:43
Corey Schafer
Рет қаралды 2 МЛН
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 45 М.