Generators in Python || Python Tutorial || Learn Python Programming

  Рет қаралды 102,955

Socratica

Socratica

Күн бұрын

𝙎𝙩𝙖𝙮 𝙞𝙣 𝙩𝙝𝙚 𝙡𝙤𝙤𝙥 𝙄𝙉𝙁𝙄𝙉𝙄𝙏𝙀𝙇𝙔: snu.socratica.... Learn how to use generators in Python. We'll cover two types: Generator Functions and Generator Expressions. Generator functions use the "yield" keyword, while generator expressions look like a "tuple comprehension." Generators are a powerful and efficient tool for use when looping over large amounts of data.
We were able to bring you this video thanks to our generous Kickstarter backers! Find their names hidden (okay, not very hidden) in the video!
Socratica Friends, we have a quiet little email group for Python if you'd like to receive updates (can't count on YT for notifications). Sign up here: snu.socratica....
⧓⧓⧓⧓⧓
Subscribe to Socratica + bell for notifications:
bit.ly/Socratic...
Find our programming playlists here:
Python programming: bit.ly/PythonSo...
SQL programming: bit.ly/SQL_Socr...
⧓⧓⧓⧓⧓
We recommend:
How to Be a Great Student
ebook: amzn.to/2Lh3XSP
Paperback: amzn.to/3t5jeH3
Kindle Unlimited (read free): amzn.to/3atr8TJ
Python Cookbook, 3rd edition
amzn.to/3goRmbG
The Mythical Man Month - Essays on Software Engineering & Project Management
amzn.to/2tYdNeP
Shop Amazon Used Textbooks - Save up to 90%
amzn.to/2pllk4B
⧓⧓⧓⧓⧓
Join this channel to get access to perks:
/ @socratica
Support Socratica on Patreon:
/ socratica
⧓⧓⧓⧓⧓
Python instructor: Ulka Simone Mohanty (@ulkam on Twitter)
Written & Produced by Michael Harrison
#Python #Generators #Coding

Пікірлер: 184
@Socratica
@Socratica 2 жыл бұрын
Wear a Socratica Python shirt for good luck coding: shop.socratica.com/products/python-by-socratica
@THEMATT222
@THEMATT222 2 жыл бұрын
Noice 👍
@satoriblue1349
@satoriblue1349 2 жыл бұрын
I've been watching your videos for about 5 years now and I just want to let you know that I now have a great full-time job as a consultant and am invaluable to my team thanks to my ability to code, which in part is thanks to you!
@skwerone
@skwerone Жыл бұрын
I got a question. I got an unusual tech Stack and don't know what position I should be submitting to. I know how to code in python mainly scraping, organizing data, sending API requests, formatting texts. I also know SEO, copywriting, creating websites and also 3D modelisation and motion graphics. I'm good at all what I mentioned.
@anonfourtyfive
@anonfourtyfive Жыл бұрын
@@skwerone Hi, You're currently in the same path that I am.. Just saying. We are searching the same things ( Probably more people than we think are trying ) I'm now speaking Python, and wish to find people to speak with. If you want, respond me back, I use selenium with tkinter GUI strap in a beautiful OOP structure, You'll understand. Kindly, Guillaume. ( William )
@jay_wright_thats_right
@jay_wright_thats_right 10 ай бұрын
@@skwerone can't anyone tell you what to do. Learn who you are and what you love to do then research multiple roles. I can cook but that doesn't mean I want to be a chef.
@ekandrot
@ekandrot 3 жыл бұрын
To speed up the prime generator, don't add 2 to cache, because all numbers tested are odd and therefore compare false. In the inner loop between line 13 and 14, break if n < p*p - this will have the biggest speedup, because it will only check up to the sqrt of n. So 3 would be added without checks, 5 through 9 will only compare against the number 3. 11 through 25 only using numbers 3 and 5. Etc. vs using all of the numbers in the cache to find each new prime.
@markrobinsontraining
@markrobinsontraining 3 жыл бұрын
I've learnt so much from your videos; they are as informative as they are fun. Thanks so much!
@rio_agustian_
@rio_agustian_ 3 жыл бұрын
FINALLY! Another python tutorial
@andrewballr
@andrewballr 3 жыл бұрын
These videos are absolutely brilliant.I like the format, short easily digestible videos on a very specific feature of python.
@murphygreen8484
@murphygreen8484 3 жыл бұрын
I look forward to contributing to the acquisition of a human
@Socratica
@Socratica 3 жыл бұрын
We know they're out there!! 💜🦉
@Socratica
@Socratica 3 жыл бұрын
You can "stay in the Python loop" by signing up for our Python newsletter: snu.socratica.com/python
@anujkondhalkar9776
@anujkondhalkar9776 3 жыл бұрын
All I have to say that those python videos from your channel are best. Would you make a complete course laylist from beginner to advanced. Some time it feels like you made it because you get pleasure frome it. Thank You for your efforts.
@danielbadra9447
@danielbadra9447 2 жыл бұрын
No programing tutorial channel has such well written and engaging videos.
@АлексейБаскинов
@АлексейБаскинов 3 жыл бұрын
A similar Haskell tutorial would be both a challenge for Socratica and a valuable source of knowledge for those who want to learn the language.
@lxathu
@lxathu 3 жыл бұрын
Never was so much owed by so many to so few for so short but so informative videos.
@Socratica
@Socratica 3 жыл бұрын
💜🦉
@vadymvv
@vadymvv 3 жыл бұрын
I'm not a noob in programming, but i was strugle to understand yield in functions. Thanks to you all is clear now!
@Socratica
@Socratica 3 жыл бұрын
This is wonderful to read, thank you for telling us! 💜🦉
@sadhappy8860
@sadhappy8860 Жыл бұрын
It's odd isn't it, with programming someone will say the right words or use the right analogy for it to make sense. That table Tennis analogy is great!
@laurentgauthier8742
@laurentgauthier8742 3 жыл бұрын
Those python videos and their slightly different approach are always crystal clear and interesting. Thank you very much for your work!
@s-code-b
@s-code-b 11 күн бұрын
This is my first ever encounter with Socratica (I think). First, thanks to Socratica for the educational content & for the quality production. Secondly, thank you additionally for inviting a Taelon to deliver the lecture. It's been the most flabbergastingly surprising delivery. I almost ran for cover😳. "Earth: Final Conflict" (SciFi by Gene Roddenberry) introduces the Taelon (alien) race to the human race on their arrival on Earth. I thought for a second it was either Da'an or Zo'or up there. I didn't know what to do. To makes things worse, she says @6:28, "This will only end when the last star burns out or we run out of memory, whichever happens first." Where's my blanky🥴? One wouldn't dare nod-off in lecture with a teacher like her. But, seriously, grateful for the the quality content.
@theepicguy6575
@theepicguy6575 3 жыл бұрын
that patreon addition was real smoooth! very infomative nonetheless. did expect yield with somewhere cause i stagger there
@MrOvipare
@MrOvipare 2 жыл бұрын
I've known Socratica with the excellent abstract algebra videos, glad to see that the Python series is just as good!
@stuarthys9879
@stuarthys9879 3 жыл бұрын
I had never considered generating generators, but if I generate any ideas I will iterate until I return a tractable solution. Thanks for the video. I appreciate the ones that are aimed higher than beginner level!
@AlbertoAvilaGarcia
@AlbertoAvilaGarcia 5 ай бұрын
embedded humor in these videos is proportional to the love they cook them with 🥰
@rrahll
@rrahll 3 жыл бұрын
best artificial intelligence visualization in the world. Thanks for the tutorial )
@youssefelamrani7905
@youssefelamrani7905 3 жыл бұрын
Shit since I started python i've never understood generators like this, it's amazing that we have Socratica to be honest thank you so freaking much
@BobP-ks1mu
@BobP-ks1mu Жыл бұрын
So happy I found your channel. Great video...I like your approach to teaching and how you break things down and provide several examples. The information provided has greatly improved my understanding!
@GIULI4994
@GIULI4994 6 ай бұрын
this is the best video I've watched about generators! such an important but somehow slippery concept to explain
@BrianSwatton
@BrianSwatton 8 ай бұрын
I really enjoy the way these are presented. Clear and informative too.
@Ghostaladdin
@Ghostaladdin 2 жыл бұрын
Your videos really are the best - I have watched them over and over again as they are as entertaining as they are educative. Really really learned a lot. Thank you thank you for all the work you put into creating this masterpiece (officially my favorite videos on coding hands down!) One quick ask, if you'll indulge me - could you possibly make something on Linked Lists, Binary Trees (in that order). Seen so much content on these but I bet hearing it from you would make it that much easier to digest/understand. Looking forward to this.
@sibyjoseplathottam4828
@sibyjoseplathottam4828 2 жыл бұрын
I didn't even know that there were Python features like generator expressions! Thank you Socratica!
@pokerchannel6991
@pokerchannel6991 Ай бұрын
I love the production process. I love you
@serta5727
@serta5727 3 жыл бұрын
It seems some knowledge was generated in my head :)
@nirajabcd
@nirajabcd 3 жыл бұрын
seriously these videos are on another level..
@Tuxiswatching
@Tuxiswatching 3 жыл бұрын
The prime generator will be way more efficient, if you only check until the square root of n is reached.
@Daniel-ng8fi
@Daniel-ng8fi 3 жыл бұрын
yeah, that would be a big speed up. Also (this wont really have any effect) you don't need 2 in the prime_cache as every number you are testing is odd
@kquat7899
@kquat7899 2 жыл бұрын
This was covered in an earlier video in the series.
@PunmasterSTP
@PunmasterSTP 2 жыл бұрын
Generators in Python generating interest. The amount of education in Socratica's videos Is indeed intense. Expense -s seem, Like what they have a lot. Let's help them out And give it a thought. Bot, Is what they're not using. Care, Is what they give. If they kept making videos till the end of time, Then knowledge would always live!
@jemzhatfelid5522
@jemzhatfelid5522 2 жыл бұрын
I'm pretty new to python and I'm trying to learn more about the language. This video was super well done and easy to understand, and I learned exactly what I wanted to. Also that circuit board dress is so cool!
@Jawahar-Engr.
@Jawahar-Engr. 2 жыл бұрын
Thank You AI Teacher... Now iam Confident enough in dealing with python generator 😃❤
@jhuyt-
@jhuyt- 3 жыл бұрын
Instead of using a sentinel value in the inner for loop of the prime generator, you can use the `for ... else` construction. It's relatively unknown but it's perfect for this
@SanataniAryavrat
@SanataniAryavrat 3 жыл бұрын
Good to see "Ulka Mohanty", the robotic actor
@snoopstp4189
@snoopstp4189 2 жыл бұрын
willing to admit Ulka is half the reason I watch these...
@vikranttyagiRN
@vikranttyagiRN 3 жыл бұрын
Always great tutorials from this channel. Really appreciated
@shudiptosekharroy651
@shudiptosekharroy651 3 жыл бұрын
Thanks!
@Socratica
@Socratica 3 жыл бұрын
Thank you for your support, kind Socratica Friend!! 💜🦉
@IvanTheChemist
@IvanTheChemist 3 жыл бұрын
I wanted this video like a month ago! Great job and a bunch of thanks!
@mihalynemes4243
@mihalynemes4243 3 жыл бұрын
A small optimization for the generation of prime numbers: instead of storing the found ones in a list, you could put them in a set.
@stuarthys9879
@stuarthys9879 3 жыл бұрын
I’m curious, why would this improve performance?
@mihalynemes4243
@mihalynemes4243 3 жыл бұрын
@@stuarthys9879 It is much quicker to look up a member in a set than in a list.
@Daniel_WR_Hart
@Daniel_WR_Hart 2 жыл бұрын
That's only if you're looking for the presence of one specific item. All found primes need to be checked anyways to verify the current number, so it wouldn't matter in this case.
@mihalynemes4243
@mihalynemes4243 2 жыл бұрын
@@Daniel_WR_Hart Yes, you're right!
@raphaelprates1500
@raphaelprates1500 Жыл бұрын
@KnightAmine
@KnightAmine Жыл бұрын
OMG ! Thanks alot! Clearer, it could not be ! 🙏
@conzymaher
@conzymaher 11 ай бұрын
Very helpful video and the style is hilarious. love it! Subscribed
@abyss2200
@abyss2200 3 жыл бұрын
Got a bit late, was so tired lately How's Socratica💝
@Daniel_WR_Hart
@Daniel_WR_Hart 2 жыл бұрын
To answer the question weighing on everyone's mind, it is possible to create a generator that generates generators: import itertools def gens(): yield itertools.count(1, 5) yield itertools.count(10, 3) yield itertools.count(20, 2) for gen in gens(): for x in gen: print(x) if x > 30: break
@Reep21
@Reep21 3 жыл бұрын
I've learned so much from this channel
@bugsolar4610
@bugsolar4610 2 жыл бұрын
Love and learnt so much from your videos, Want videos on Multithreading, multiprocessing, Class and static methods, public private and protected methods, super keyword.
@MJ-em_jay
@MJ-em_jay 3 жыл бұрын
I like the humor in these videos.
@volbla
@volbla 11 ай бұрын
I recently found out about the "yield from" expression. You can use it to create generators with a recursive definition.
@kirbymarchbarcena
@kirbymarchbarcena 3 жыл бұрын
ME: Another great lesson, what can you say about this? PYTHON: Hissssssss!
@skilz8098
@skilz8098 3 жыл бұрын
How did a python turn into a hydra? Generators!
@kenhaley4
@kenhaley4 3 жыл бұрын
Never knew about the generator expression. Thanks!
@piotrmazgaj
@piotrmazgaj 2 жыл бұрын
This is gold!
@muntedme203
@muntedme203 2 жыл бұрын
What a sense of humour...love it 😆
@daniellow426
@daniellow426 3 жыл бұрын
Learning a lot from you folks. Thank you.
@karim-gb5nx
@karim-gb5nx 2 жыл бұрын
This is actually impressive
@bass9052
@bass9052 2 жыл бұрын
Happy to see you again
@yonaguska2050
@yonaguska2050 3 жыл бұрын
I've missed these tutorials
@amauta5
@amauta5 3 жыл бұрын
I always love this series!
@chris9352
@chris9352 3 жыл бұрын
Oh, she's back!
@ehudkotegaro
@ehudkotegaro Жыл бұрын
maybe instead of looping over the primes in primecache you can define a variable fact=2 before the first loop, that will contain (n-1)! Than to check if n is prime we check weather (fact+1)%n==0, and to get the next factorial we need to multiply fact by n(n+1).
@skilz8098
@skilz8098 3 жыл бұрын
I'd like to see an Assembly Tutorial from this channel!
@theblindprogrammer
@theblindprogrammer 2 жыл бұрын
What is Assembly used for these days? I thought C replaced Assembly
@skilz8098
@skilz8098 2 жыл бұрын
@@theblindprogrammer It can be used for a lot of things, especially when you are designing your own ISA...
@AhmadAsmndr
@AhmadAsmndr 3 жыл бұрын
I am very happy to see you again with another amazing video. thank you very much
@FilterYT
@FilterYT 2 жыл бұрын
I would like to compliment the presenter, thank you.
@MaximoPower2024
@MaximoPower2024 3 жыл бұрын
Why do you call close() method instead using break in order to stop the for loop?
@ilkhomibrokhimov4729
@ilkhomibrokhimov4729 3 жыл бұрын
Thanks
@Socratica
@Socratica 3 жыл бұрын
Thank you for your kind support! We're so glad you're watching!! 💜🦉
@alurma
@alurma 3 жыл бұрын
Awesome video, didn't know about generators. Thanks!
@RichardGreco
@RichardGreco 3 жыл бұрын
Another great python tutorial. You should develop another way to accept financial support. I know someone is taking a big cut somewhere and I'd rather not give them anymore right now.
3 жыл бұрын
Awesome and helpful video! Great work 🥰
@mrmuranga
@mrmuranga 3 жыл бұрын
Thank you..always looking forward to the next video....
@thinboxdictator6720
@thinboxdictator6720 3 жыл бұрын
I remember computerphile did something like this on yield primes: def nats(n:int=2) -> int: """Natural numbers.""" while True: yield n n += 1 def sieve(s): n = next(s) yield n yield from sieve(i for i in s if i % n != 0) primes = sieve(nats()) MAX = 1000 for number in primes: print(number) if number > MAX: break but recursion limit breaks it with higher MAX value
@MrEgol
@MrEgol 3 жыл бұрын
Grazie.
@Socratica
@Socratica 3 жыл бұрын
Many thanks for your kind support!! It's such a help. 💜🦉
@brpawankumariyengar4227
@brpawankumariyengar4227 3 жыл бұрын
Great Video …. Thanks …. I learn python from your videos
@thegenxgamerguy6562
@thegenxgamerguy6562 2 жыл бұрын
How to speed up: 1. Don't put 2 into the cache, because iterating 3 by 2 never results in an even number 2. Use the fact that all prime numbers greater than 3 are congruent to 1 or 5 mod 6 (you can massively speed up) 3. Only iterate up to square root of (n) when checking for primeness This will give you a massive speedup.
@hackerxxspacex6935
@hackerxxspacex6935 2 жыл бұрын
Can you please explain point number 2 and 3.
@thegenxgamerguy6562
@thegenxgamerguy6562 2 жыл бұрын
@@hackerxxspacex6935 Of course. Ad 2): All prime numbers > 3 are either 6k+1 or 6k-1 where k is a natural number. Examplea: 7 = 6 * 1 + 1 11 = 6 * 2 - 1 It's important though that of course not all numbers of one of those forms are prime, but you can exclude a large portion of the natural numbers from the division (actually: modulo) testing. I also use this to unroll my loop when I need a quick and dirty prime number test, because I do loop = loop + 6 and then the two divisions (sieving only requires prime numbers as divisors). Saying they are congruent to either 1 or 5 mod 6 is the same sentence, just shorter. Oh, and also I do a preliminary check if n mod 6 is either 1 or 5 to weed out many n before even starting to loop. Thats helpful, too. Ad 3): If you want to test n for primeness you only need to do division testing up to the square root of n (sqrt(n)) because sqrt(n) * sqrt(n) is already n.
@gvimvim
@gvimvim Жыл бұрын
this footage is awesome
@ChrisHalden007
@ChrisHalden007 3 жыл бұрын
Great video. Thanks
@__raghu.vir_
@__raghu.vir_ 3 жыл бұрын
Good job, ❤️from india
@alexgora7530
@alexgora7530 2 жыл бұрын
LOVE YOUR WORK!!!
@crazywallcat
@crazywallcat 9 ай бұрын
This is so good!
@RachidMarzouki
@RachidMarzouki 3 жыл бұрын
Excellent
@dw2291
@dw2291 Жыл бұрын
Great vid, a lot of fun.
@ssh17hx0r
@ssh17hx0r 3 жыл бұрын
This is fantastic!
@mawkuri5496
@mawkuri5496 3 жыл бұрын
i hope you create a machine learning tutorials using python. that would be awesome.. and math for machine learning separated playlist
@marcin2x4
@marcin2x4 2 жыл бұрын
Can we do in the next video following concepts? - name mangling (__double_leading_underscore name for class attribute) - @staticmethod - decorators - _single_leading_underscore
@bckzilla
@bckzilla 3 жыл бұрын
Very nice.
@Sneaky-Sneaky
@Sneaky-Sneaky 2 жыл бұрын
I joined this channel primarily to watch your Python videos. They are relaxing and stimulating simultaneously. After 20 years as a software engineer, it is quite unexpected ! For each video() Smile = Smile + 100 Next Print(Smile) 3700. #my face hurts ! Thank you! ….. my I have another please ? If answer = “Not now or in any other space in time” Then 😢😭 Else 🖖🏼
@douglassoaresmantova
@douglassoaresmantova 3 жыл бұрын
I love this channel
@giftfromyoutube
@giftfromyoutube Жыл бұрын
I just so much love her...😃
@ubertcoolie8694
@ubertcoolie8694 3 жыл бұрын
Thanks for the video, it is great!
@hanfmampf1234
@hanfmampf1234 3 жыл бұрын
Seems like magic to me
@qizhenggu20
@qizhenggu20 3 жыл бұрын
“is_prime” in code could be replaced by a for-else statement
@MIKIBURGOS
@MIKIBURGOS Жыл бұрын
6:08 how is a generator expression different than a set syntactically? Because I would assume (x**2 for x in y) is a set (and therefore store all those numbers in your memory)
@sayanghosh6996
@sayanghosh6996 11 ай бұрын
do you mean tuple? sets are enclosed in { }
@sebavergarasassi6462
@sebavergarasassi6462 2 жыл бұрын
¡Gracias!
@Socratica
@Socratica 2 жыл бұрын
Thank you for your kind support!! 💜🦉
@omarcruz6326
@omarcruz6326 3 жыл бұрын
Python Videos are back ! 😍
@nullsemicolon
@nullsemicolon 3 жыл бұрын
Great video!
@yonatantre
@yonatantre 3 жыл бұрын
please please make a js tutorial u r the best
@SergiiStarodubtsev
@SergiiStarodubtsev 3 жыл бұрын
very nice!
@ozonefriendlyman
@ozonefriendlyman 2 жыл бұрын
"Pleasantly more finite" :)
@infinityinf1
@infinityinf1 3 жыл бұрын
Yes, it is possible to generate generators that generate generators.
@alinaderiparizi7193
@alinaderiparizi7193 6 ай бұрын
another awesome!
@Hardzinho_yay
@Hardzinho_yay 2 жыл бұрын
The most important thing Ilearned in this video is how 'yield' is properly pronounced
@coolfreaks68
@coolfreaks68 3 жыл бұрын
*Is Socratica related to Socratis in some way ?* 😁
@TheWyrdSmythe
@TheWyrdSmythe 3 жыл бұрын
Great last lines! 😂
Python Generators Explained
28:37
Tech With Tim
Рет қаралды 157 М.
Это было очень близко...
00:10
Аришнев
Рет қаралды 6 МЛН
Friends make memories together part 2  | Trà Đặng #short #bestfriend #bff #tiktok
00:18
Python Generators
15:32
mCoding
Рет қаралды 139 М.
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 188 М.
SQLite in Python || Python Tutorial || Learn Python Programming
10:11
Special Methods  ||  Python Tutorial  ||  Learn Python Programming
12:23
Learn Python OOP in under 20 Minutes
18:32
Indently
Рет қаралды 74 М.
Data Types in Python | Python for Beginners
21:58
Alex The Analyst
Рет қаралды 120 М.
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 894 М.