Loop like a native: while, for, iterators, generators

  Рет қаралды 117,652

Next Day Video

Next Day Video

Күн бұрын

Ned Batchelder
Python provides powerful primitives for iterating over your data in ways that let you express yourself clearly and directly. But even programmers familiar with the tools don't use them as fully as they could. This talk will cover Pyt

Пікірлер: 99
@fatihakilma2098
@fatihakilma2098 4 жыл бұрын
"So my name is Ned Batchelder and I'm not gonna tell you who I am. You can google me if interested. Let's get into the meat of the talk." What a great introduction... People should learn to google first and then ask the question if needed. Time is gold.
@rameshsampath6998
@rameshsampath6998 11 жыл бұрын
Best talk of PyCon for me. Clear, concise and reveals the power of Python. Don't just write code, write pythonic code. Thanks Ned.
@joseville
@joseville 2 жыл бұрын
Just taking notes: 3:47 those `dict` methods were renamed to `.values()` and `.items()` 10:43 I already knew what zip does but "zip takes a pair of streams and gives you a stream of pairs" is such an elegant succinct explanation. 20:32 Now you can just use `for x, y in itertools.product(range(height), range(width)):` 21:32 "A book is an iterable; a bookmark is the iterator -- you can have two bookmarks in the same book" - great analogy! 22:30 files are iterators too
@PippyPappyPatterson
@PippyPappyPatterson 2 жыл бұрын
good notes
@tolpacourt
@tolpacourt 9 жыл бұрын
I'm so glad youtube allows me to slow down the playback. He covers so much ground so quickly.
@SwastikSwarupDas
@SwastikSwarupDas 5 жыл бұрын
@The Magnificent Whalens i guess this talk wasnt meant for beginners, i found it rather helpful, after having a week of python and 2 years of C
@Post1690
@Post1690 6 жыл бұрын
I have watched this so many times over the last few months - if you know someone who's teaching him/herself Python, I'd highly recommend you share this!
@pluckthelivingflowers
@pluckthelivingflowers Жыл бұрын
Easily the best explanation of these topics I’ve seen.
@Guinhulol
@Guinhulol 4 жыл бұрын
That's my kind of python talk, he just go right to the chase.
@MuneerKhan1
@MuneerKhan1 5 жыл бұрын
Probably one of the very few videos that don't let you blink! 🤙🏻
@yztyzt1
@yztyzt1 7 жыл бұрын
I love how clean and aesthetically pleasing compact python code is! OMG> I am in love with the beauty of this language!
@edterrell5664
@edterrell5664 4 жыл бұрын
Learned more about loops and itersables in the first 5 minutes than in the past two years studying python!!!
@pablom695
@pablom695 3 жыл бұрын
this guy is a fucking genious. you can see it espcially at the end when answering questions
@hangugeohaksaeng
@hangugeohaksaeng 10 жыл бұрын
Thanks for this great talk. Just finished watching for the second time. Much to get out of this. Thanks again!
@szaydel
@szaydel 11 жыл бұрын
Great talk, covers what we do all the time, and mostly badly, and teaches us how to do it better!
@user-tb4ir5ny8n
@user-tb4ir5ny8n 7 жыл бұрын
Very good and densely packed info for beginners. Thank s for a great job.
@MrTigerstyle80
@MrTigerstyle80 11 жыл бұрын
This guy knows his business.
@drygordspellweaver8761
@drygordspellweaver8761 2 жыл бұрын
PURE GOLD especially making your class objects iterable
@H0r4c3
@H0r4c3 2 жыл бұрын
Clear and concise explanations! Thank you!
@curiousbit9228
@curiousbit9228 6 жыл бұрын
Wow, Mindblown! Have to watch it like 20times to actually understand it.
@zeroxcub
@zeroxcub 7 жыл бұрын
"your code has interesting streams of values that you are not producing in a nice iterable form yet" Thanks, really nice talk.
@janhutar
@janhutar 10 жыл бұрын
Thanks. Very nice talk - now I'm confused about generators/iterators/iterables for sure, but I guess I can actually use them :-)
@edt11x
@edt11x 5 жыл бұрын
Great video. It is jammed with useful information for me.
@paddym27
@paddym27 4 жыл бұрын
really nice presentation - cheers!
@meunomejaestavaemuso
@meunomejaestavaemuso 9 жыл бұрын
20:50 with generators you can use the 'send' method, so you can 'ask' for a value using 'generator.send(desired_value)' and having 'while True: x = yield; yield x' inside your generator, useful to make co-routines. Another useful thing about generators in Python 3.3 or higher is the 'yield from' statement which can link several generators and yield values from them.
@joseville
@joseville 2 жыл бұрын
Wow, I didn't know about this! Now, I need to go and learn about it because it looks very usefule.
@drygordspellweaver8761
@drygordspellweaver8761 2 жыл бұрын
More on this please
@nickmcneely5601
@nickmcneely5601 7 жыл бұрын
Question: For the evens function, why is a generator better than a list comprehension? Answer: For small inputs, it's not that big of a deal. For big lists, generators use muuuuuuuuch less memory.
@Lolwutdesu9000
@Lolwutdesu9000 6 жыл бұрын
Nick McNeely thanks, the guy in the video could have mentioned this.
@ZapOKill
@ZapOKill 4 жыл бұрын
so lets use generator comprehensions
@yoganandchowdary6371
@yoganandchowdary6371 7 жыл бұрын
one of the bestttt tutorials....it helped me a lot.....thankyou!!!!!
@Neceros
@Neceros 7 жыл бұрын
13:40 so basically, try and keep all of the choosing code in the head of the loop via a function and not in the body. Smart and clean!
@yztyzt1
@yztyzt1 7 жыл бұрын
it looks like you can iterate over a list of x values, send that x value to a function. Then, put the result of that function into a list of results. It look like very clean code!
@smlasdhaosf7764
@smlasdhaosf7764 8 жыл бұрын
Always thought this talk should belong to Raymond Hettinger, well, Ned did a good job
@shahrushabh87
@shahrushabh87 9 жыл бұрын
Nice video Ned.. Thanks :)
@aliabedi6163
@aliabedi6163 3 жыл бұрын
It was very beautiful and informative (2021)!
@pradeepnim
@pradeepnim 9 жыл бұрын
Good one!! clean and fast!
@yztyzt1
@yztyzt1 7 жыл бұрын
you don't need range of len??? I can't wait to see what you say, so that i can learn from you and get rid of the integer. (I am guilty of using range of len) and I do think it is very ugly. I don't like it. I paused at 8:00 to type this confession. Now I am going to unpause in order to see how I can get rid of all range(len) cases! I am excited!
@maguas01
@maguas01 5 жыл бұрын
Learned a lot today
@TommyCarstensen
@TommyCarstensen 11 жыл бұрын
Awesome talk! Thanks for uploading! Ned should replace the image of Superman with an image of himself!
@PhilippeSantanaCosta
@PhilippeSantanaCosta 11 жыл бұрын
Cool! Nice stuff...
@amaterasu48
@amaterasu48 6 жыл бұрын
Wow this guy knows his stuff!
@dlednirg
@dlednirg 10 жыл бұрын
Outstanding presentation! Any chance you could point me to a link of your slides. Trying to get my head around Python and OOP and your talk really hit some points NOT covered in my other sources. Thanks much
@mangelozzi
@mangelozzi 5 жыл бұрын
Great presentation in terms of conveying lots of info quickly, cleanly, and easy to understand. I learnt a lot thank you. One confusing thing for myself I found at 21:19 On the slides: iterable: produces an iterable iterator: produces a stream of values But he says: iterable: Any value that can produce a stream of values iterator: Is the object which knows where you are in that stream. The wording is confusing, the "slides iterator" definition sounds like the "spoken iterable."
@renyuanzhang9706
@renyuanzhang9706 5 жыл бұрын
What he said is correct, and what on the slides is: iterable: produces an iterator # not iterable iterator: produces values off a stream one at a time # more precise way to put it
@Murkz21
@Murkz21 3 жыл бұрын
@@renyuanzhang9706 If I put a Pass down the def __iter__(self):, then what will happen? Is it still ok to use for the for/while loop?
@swapnagupta6756
@swapnagupta6756 4 жыл бұрын
At 20:55, is spreadsheet.cells() a built-in function in Python? If not , is it just a generator with 2 for loops on rows and columns returning cell values?
@RaymondJerome
@RaymondJerome 5 жыл бұрын
to break out of nested loops I, as a BASIC programmer for well over 35 years I use GOTO. the only time i ever use goto. not elegant but practical. I love all the 'new' thinking python has taught me.
@vayunandu
@vayunandu 5 жыл бұрын
pure gold
@ZenKloom
@ZenKloom 5 жыл бұрын
Python is beautiful.
@TommyCarstensen
@TommyCarstensen 11 жыл бұрын
11:15 "zip is a function that takes iterables and produces an iterable" Ned ignores the existence of Python 2 and itertools.izip :)
@MatthewTaylorAu
@MatthewTaylorAu 3 жыл бұрын
I get the concept, in my newb days I had a mental block over readability. Is there a speed/resources benefit?
@mangelozzi
@mangelozzi 5 жыл бұрын
At 12:32 what is the advantage to using .get in the key function? Is it not adding an extra dict look up, instead of accessing the tuple directly? (k, v) being passed into the .get(), so it's like: seq.get(k, v) k is treated as the key, and v is as the default value if not found. Am I the only one who finds its a bit more obscure?
@matthewherzog6155
@matthewherzog6155 8 жыл бұрын
This is for Python 2.x. For example, when he uses _print repr(line)_ it will throw an error in Python 3.
@vishnuvarthan8304
@vishnuvarthan8304 6 жыл бұрын
yeah right concepts matter
@yomajo
@yomajo 4 жыл бұрын
14:20 - very nice.
@736939
@736939 6 жыл бұрын
At 14:00 in function should be append(n) not append(x)
@RamkrishanYT
@RamkrishanYT 5 жыл бұрын
Can anyone explain the second command at 12:36 key=lambda b:b[1]
@deepakuttam5435
@deepakuttam5435 5 жыл бұрын
tall_building.items will return the tuple of (buildingname, height), we are sorting this by the height , so lambda b : b[1] will take tuple as input (buildingname , height) and will return the height i.e b[1] as key value for sorting . if we want to sort by the buildingname it would have lambda b :b[0].
@Will_Keatinge
@Will_Keatinge 8 жыл бұрын
The entire function range_2d in 20:23 could be replaced with itertools.product
@jazej5516
@jazej5516 8 жыл бұрын
my teacher wants us to iterate the first 4 digit sum to a single digit. How would i go about this? His hint was # can you iterate over a string or a list or a tuple? mylist=list("MyVeryOwnString") for counter1 in mylist: print(counter1) # can you iterate over a number? mynumber=12345 for counter2 in mynumber: print(counter2) Next Previous so im assuming you cant itterate a number so somehow i would need another string but that is where i am stuck...
@ericmacleod8605
@ericmacleod8605 8 жыл бұрын
+JazeJedikai convert your integer to a string then slice print str(yournum)[3]
@jazej5516
@jazej5516 8 жыл бұрын
hmm i did it differently. This seems simpler though lol
@MrSkatefuture911
@MrSkatefuture911 3 жыл бұрын
I am fairly new to Python and I am wondering if anyone with more in-depth knowledge could tell me to what degree these speeches from 5-10 years ago are still applicable to current version of Python? for replier in repliers: print(f"Thank you {replier}")
@joseville
@joseville 2 жыл бұрын
They're very applicable still. Some of the function names have changed, but still very applicable.
@me4901
@me4901 11 жыл бұрын
Genius within 2:25! Integers? if you want to print your list, just print your list!
@bmurph24
@bmurph24 9 жыл бұрын
Cool lecture I'm guilty of some of the horrible looking needlessly complex loops he mentioned. It's a shame how much time this probably could have saved me, especially the generators.
@kalidasa_in
@kalidasa_in 11 жыл бұрын
Expert users should start at 14:00.
@mctrjalloh6082
@mctrjalloh6082 3 жыл бұрын
what tool is he using for presentation content?
@nedbatchelder
@nedbatchelder 3 жыл бұрын
It's not a convenient tool, it's a hand-rolled tool-chain: nedbatchelder.com/blog/201504/how_i_make_presentations.html
@RobertLugg
@RobertLugg 10 жыл бұрын
Good basic talk.
@enyoc3d
@enyoc3d 8 жыл бұрын
22:18 note to self: use this in your CSV code
@andrewsanchez5895
@andrewsanchez5895 7 жыл бұрын
>>> def hi_world(): ... yield "Hi" ... yield "world" Why does next(hi_world()) always produce "Hi"? but you get: >>>for x in hi_world(): ... print(x) Hi world
@Grelleum
@Grelleum 6 жыл бұрын
Because each time you use next(hi_world()) you are creating a new iterable object. The for loop is reusing the same iterable object. Instead try instantiating a new generator object: >>> my_hi = hi_world() >>> next(my_hi) 'Hi' >>> next(my_hi) 'world' >>> next(my_hi) Traceback (most recent call last): File "", line 1, in StopIteration >>>
@pgoeds7420
@pgoeds7420 5 жыл бұрын
8:30 s/Sears/Willis/
@marshabner7731
@marshabner7731 4 жыл бұрын
Found typo for variables at 14:19: "them.append(x)" ==> "them .append(n)"
@tommyli6916
@tommyli6916 7 жыл бұрын
6:26 said: for list function will take an iterable and will pull all the values out of it and give you a list of all of them. Don't use this on itertools count because you will run out of time. Why ? ??
@jimstanley_49
@jimstanley_49 7 жыл бұрын
Because itertools.count() generates an infinite stream. new_list = list(itertools.count) will loop forever (or until you run out of memory) as it tries to generate a list of all integers from 0 to infinity.
@RonJohn63
@RonJohn63 7 жыл бұрын
This is a very interesting video. 10:51 zip is possibly the Worst Name Possible for this function, without resorting to politics. The word already has a specific meaning.
@kipsta1993
@kipsta1993 7 жыл бұрын
may somebody please explain the joke at 12:00 :D
@Grelleum
@Grelleum 6 жыл бұрын
"Python can loop tall buildings in a single bound" is a play on Superman can "Leap tall buildings in a single bound."
@owaisgen
@owaisgen 6 жыл бұрын
HI I am getting error dictTower=dict(zip(names, heights)); TypeError: zip argument #2 must support iteration... Are you getting some error?
@Zeturic
@Zeturic Жыл бұрын
This is an excellent talk, but I generally hesitate to refer it to beginners because of the Python 2-isms. Getting into the minutia of how Python 3 and 2 are different can be interesting, but I feel it's just an unnecessary distraction for beginners who are starting with Python 3.
@ThunderAppeal
@ThunderAppeal 10 жыл бұрын
Not sure why there is so much praise for this talk. He spends about 18 minutes talking about various loops, all well and good. His presentation on generators is weak at best and misrepresented realistically speaking. At 18:05 he shows the pre-generator example, at 18:19 he takes out the comments, tightens up the lines and puts a yield rather than the do_something return. This indicates to me that poor little Ned is having difficulty truly explaining the diffrences between a generator and a regular loop. At least he is able to cover zip with some understanding so good on you Neddy. This should serve as an indication of what is wrong with the software development industry today. A lot of frauds running around with the talent of being able to pull off the appearance of understanding what they are doing. Hello to all the python and nedbat fan boys.
@ThunderAppeal
@ThunderAppeal 10 жыл бұрын
Took out comments to make it seem as if the generator uses less lines of code. All generator does is yield an individual result. Generators eat up more CPU time, versus a regular loop that may eat up more memory. With clusters...I'm sorry I mean clouds all that may be a moot point depending on what someones subscription plan to the cluster is.
@Lolwutdesu9000
@Lolwutdesu9000 6 жыл бұрын
I'm not sure why he even bothered to make a separate "evens" function at around 16/17 minutes. Can't really see that working faster, or being more efficient memory-wise. Sure, making a generator sounds great, but who would actually use one in that situation?
@darrinmc
@darrinmc 5 жыл бұрын
@@Lolwutdesu9000 It makes the code easier to read
@psypher514
@psypher514 4 жыл бұрын
iter
@dokwonsuh
@dokwonsuh 2 ай бұрын
Half Python, Half Cocaine
@erichepperlewp
@erichepperlewp 2 жыл бұрын
Feels like a powerful talk ... lost me at #generators.
@voiceofprime
@voiceofprime 7 жыл бұрын
Is it my ears or he talks really fast?
Python's Class Development Toolkit
45:56
Next Day Video
Рет қаралды 237 М.
Sigma girl and soap bubbles by Secret Vlog
00:37
Secret Vlog
Рет қаралды 13 МЛН
🤔Какой Орган самый длинный ? #shorts
00:42
Каха заблудился в горах
00:57
К-Media
Рет қаралды 7 МЛН
Transforming Code into Beautiful, Idiomatic Python
48:51
Next Day Video
Рет қаралды 1 МЛН
When Booleans Are Not Enough... State Machines?
21:50
Next Day Video
Рет қаралды 24 М.
Stop Writing Classes
27:29
Next Day Video
Рет қаралды 932 М.
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 56 М.
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 797 М.
The Clean Architecture in Python
49:54
Next Day Video
Рет қаралды 108 М.
Sigma girl and soap bubbles by Secret Vlog
00:37
Secret Vlog
Рет қаралды 13 МЛН