Комментарии:
for the iterator comprehension, you can get rod of those ugly indices with
study_times = (minutes for task, minutes in events if task == "learn")
You also won't make the error to use it twice if you put it directly in the sum call (makes more concise code, too):
minutes_studies = sum(minutes for task, minutes in events if task == "learn")
😢
ОтветитьStick to the basics. Don't memorize useless stuffs.
ОтветитьOkay what is the vs code theme? Oh and good video, really helpful
Ответитьisn’t an out of range array index supposed to return undefined?
Ответитьvery interesting the itertools packages, i didnt know about it.
ОтветитьIt would be good to mention how much memory each method uses. I have seen some horrific python codes in memory requirements, compounded by the author having no idea why it uses as much memory as it does
Ответить7th tip: just use another language like C or Java
ОтветитьWhy use islice(lines, 5) instead of lines[:5] ?
ОтветитьGreat video. Very useful. Thanks
ОтветитьSyntax error on line 1 in thumbnail 🤓
Also nice vid
I learned a lot from this vid; learned even more from the comments 🐍 💬
ОтветитьWhatever man, I'm sticking with my 6 nested for loops. 🤌
Ответитьclickbait
ОтветитьNo idea about python but in every instance where a for in loop can be used it’s a lot more efficient to use a traditional for loop
ОтветитьThe thumbnail is missing a bracket
You meant to do this instead:
for i in range(len(data)):
print(data[i])
step 1: ask chatGPT
step 2: copy
step 3: paste
What about recursion? p.s. It couldn’t be true that imperative language can't deal with loops or ? 🤔
Ответитьah this video too late.. my final project already done 🥲
ОтветитьCool😊
ОтветитьWhat for should i use islice instead of [ : 5]?
ОтветитьBad advise: "write more complex code, and another programmers will spend more and more time to understand your code".
ОтветитьNoting new, but for generator and pairwise I would unpack in for like:
minutes_studied = (minutes for event, minutes in events if event == "learn")
for pair0, pair1 in pairwise(...):
Anything is better than your thumbnail code because that doesn’t even run
Ответить“This is very error-prone”: why?
ОтветитьYou know, I get all these "use native methods" things in Python, but at what point does Python become less readable because there are all these little native functions?
Wouldn't it be better to let an optimizer turn a for loop into a list comprehension? Just how, exactly, is a list comprehension always better than a for loop in terms of readability? Sure, it's better for speed, but is it better for readability?
It would be an error if Python got turned into APL (a fabulous, but write-only language).
-----
Fire is a good servant, but a bad master.
Let us have computer languages which serve us, rather than having us serve them.
Something I dealt with recently is where I wanted to loop through the lines of a text file, find the first occurrence of e.g. "hello", and then loop from that point onwards, to find the first occurrence of "random", and, only if we find "random", go back through the lines between that containing "hello" and that containing "random". Iterators make it hard to consume, and harder still to backtrack, things that are easy with C's for(i=1;i<10;i++) construct.
Another is where if an element of a sequence matches some pattern, you want to consume and process the next element of the sequence before the loop continues. (The latter is a case where you have to do for x in I := iter(something): ... y= next(I)
and so on.
It would be nice to see a video where you show genuine cases where you have to use the 'ugly' loop construct, and where things like sum and zip aren't any help.
i hate to be that guy. but it is impressive how bad python messed up how loops are implemented. when in other languages its just a goto. wonder why they did that. thank god for pypy.
ОтветитьNow sum() is better style than "for" anyway. But other than that, if these performance differences matter to you, don't use CPython. Use a python interpreter with a good JIT that can optimise away these differences rather than writing fine-tuned code for a slow interpreter.
ОтветитьGreat video. Also for an old dog good as a reminder. np is a powerful tool (like pandas) but very annoying are their type differences, e.g. None, float, etc.
ОтветитьWelp, may as well write my loop code in C/C++ and import it in python.
ОтветитьIs islice() == [::]?
ОтветитьI'm thinking lazy, and didn't get a point of using "islice" instead of standart "print(line) for line in lines[1:5:2]" slicing.
ОтветитьPairwise would've been really useful in an AoC problem lol
I really need to read up itertools and functools. There's so much obscure but handy stuff there
Abt the generator tip,
I'd argue it's better to put the generator comprehension inside the function that uses it.
1 reason is as you've shown, storing it in a variable gives the impression that it is reusable when in fact it may be exhausted in some other place.
Another is that I think it's very readable that way, e.g.
sum(
event[1]
for event in events
)
I find that it reads really well with other functions too, namely `any` and `all`, e.g.
all(
event[1] > 10
for event in events
)
It is not true, that loops are bad, as they are the place that JIT compilers can shine after all. It is all just about the python implementation
ОтветитьThanks for the tips, but tell us, what's your VS Code theme?
ОтветитьПитон это жуткий ущербный язык.
Ответитьnot sure if youve seen, but sum() does weird things with floats. for example
my_list = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
sum(my_list) = 0.9999999
if you use numpy, np.sum(my_list) = 1
Can avoid some headaches this way especially when dealing with predictions of propensity where 0.001 can matter.
would be interesting if u include benchmark number also. Performance is also vital role when we do refactoring code, besides readability. For me performance is number one becoze python it self already readable by nature.
ОтветитьThanks for your sharing
ОтветитьYou forgot range closing ')' in code in the thumbnail.
ОтветитьI also like shades of purple 💜
ОтветитьI’ll just keep it simple and use foreach(array $list as $key => &value); loop in php
ОтветитьI really don't get why people love python, I have to use it daily at work since our backend is in python, and I have zero fun using it, I feel the language is not helping me, it is in my way
ОтветитьPlease tell me which plugin did you use in the design
Ответитьvs code theme name ?
Ответитьcoding is one of the few activities where being lazy is actually helpful
ОтветитьDid not know about the strict parameter added to zip in Py3.10. Nice!
Ответить