On the other side, if you don't know the particular function (that you or someone else used) it will make the code less readable + and if you don't know all features of the functions you will get lost on finding a bug. Loops are not always bad if well designed inside a concept and using good commentary lines for descriptions/explanations.
Problem here is more deep, than you might think. This guy said "functions are optimized", but that's not a complete true. Functions can allocate extra stuff during calculations what makes such ones not optimized. Allocations could be avoided using just loops. Even if function is fully optimized you may have another flaw, because sometimes more than one operation is needed and then output from one function goes to input of another function, which now do extra calculations and may be not optimized. This is why you should not rely on those functions on 100% if you don't understand concepts of memory management and optimization
@@alexmiller3260 well the easy answer here is that partially D'Oliviero already answered you and I can say that seems you are referring to iterative and recursive functions. Of course if you are programming you have to know of how it works regarding memory management etc... It is a choice you will make at the moment and maybe refactor it along the way...
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 )
Generators are great to save RAM memory for large arrays or Sequences. Although they can be slower than looping on array due to the need of computing each next step. My go to is choose the most readable by default and optimize if you need to. As for islice, i largely prefer to use the slicing syntax in python which is simple, for exemple my_list[2:5:2] is equivalent of islice(my_list, 2, 5, 2).
It is not equivalent, the slicing syntax copy the slice while islice() is a generator that doesn't copy the array. For memory use and sometimes execution speed, islice will be better.
Strange that you haven't mentioned, but when you want to use array elements as function arguments, you can simply write *array (most useful example: print(*array) will print all of array elements separated by spaces (use argument sep=' ' to write each value into new line) )
You can do that with dictionaries as well: **dict will place (key, value) pairs in the function call as optional arguments in a key=value form. However this video is about for loops and not about arrays or other data structures, so I guess that's why function calls not mentioned.
2:25 zip() is also useful with database queries. Say you have a list or tuple of field names: you can use ", ".join(field_names) to format the field names for inclusion in a “select” statement; then call dict(zip()) on the returned tuple and the field names, to create a mapping from field names to field values.
@@lawrencedoliveiro9104 he meant psycopg2 functionality. Don’t you use it to call db queries from your python code? I don’t believe you do it using standard library
@@lawrencedoliveiro9104 got it. It’s strange that all of these connectors don’t provide this feature like psycopg2 does. It really helpes a lot in case you cannot use ORM for higher abstraction level.
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
I think the main situation you would use islice is when you want to slice a generator. For a plain list, regular slicing in the index (as you've written) is easily understandable and also probably the most efficient. But generators don't support indexing and islice supports any iterable including generators. A small caveat that I discovered after checking the docs is that islice doesn't support negative indices but regular slicing does (e.g. last 3 elements lines[-3:])
islice() creates an iterator over the initial array content while list slicing creates a copy (with Python lists, slicing on numpy arrays just creates alias, which sometimes results in strange behaviour, probably the reason why basic Python doesn't do it this way). On small slices this is not important but if you use big slices, in a nested loop, slicing may give a lot of work to your garbage collector.
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.
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")
I am falling in love with Python and coding in general. It allows us to do so much with so little effort, except writing the code in the first place, that is hectic but once the code is ready and everything works you feel like a super human.
The more you do it, the more adept you will get at it, and when you write a truly elegant and performant piece of code (be it a function, a class, or whatever), it will feel like poetry. Same with elegant mathematical proofs. I love Python, but I prefer Kotlin because it has so much elegant, built in functional programming constructs, and functional programming can take your code and shrink its length dramatically. Python has some functional programming concepts built in, but they're a bit clunky. It's still a great language, though, if you're not worried about performance (and if you are, just make sure you use built-ins and numpy, since they're implemented in C so they run much faster than loops).
@@vorpal22 Guess I gotta learn Kotlin next. But trying to get into coding, I feel like I should at least spend some time with Python more before learning another language. Although the job market requires multiple languages for us to land a job, I just feel like becoming advanced in one language could perhaps be beneficial. Any thoughts?
@@md.masumomarjashim I agree that you should focus on one language for awhile before moving to the next... if you really want to become a developer, you should have a strong working knowledge of at least one language, and then have some exposure to other languages so you cover your bases. You probably want to know some of each of the following: 1. Object-oriented programming 2. Procedural programming 3. Functional programming Python is - according to many surveys - the language that is in highest demand now (followed by Java, but it is declining), so it's a good choice. There's a book called "Fluent Python" that is the best I have ever looked at: it is a hefty one, but you don't have to read it cover-to-cover and it'll teach you a lot of Python than you probably didn't even know existed. O'Reilly just updated it for Python 3.10, so it's very up-to-date, and you can just skip around the chapters and learn what you need or what interests you. No Python developer should be without it. To learn Kotlin is a bit harder, because you do need to have some working knowledge of Java since Kotlin compiles into Java bytecode (same with Scala and Clojure, which are also both hybrid object-oriented and functional programming languages... Clojure is really messy, in my opinion, but it's interesting to dabble in because it makes you think about programming very differently). Keep in mind that if you know a couple languages, many companies aren't too concerned about what languages you know: you can learn one pretty quickly and it's a small investment on their part. The best thing to do is show that you have problem solving skills. There is a website called "daily coding problem" that is really valuable and covers lots of questions you'd encounter in an interview. You can sign up for free to get the problems delivered to your inbox daily (they are ranked by easy, medium, and hard), and if you pay a small fee, you can also get the best solutions to the problems the next day. Another thing that is becoming more and more popular in the hiring process is having a GitHub account with a bunch of repositories that you're working on: either personal projects, or contributions to open-source projects (I find that a bit more intimidating), school assignments, etc. It's like a programmer's equivalent of an artist's portfolio. If you don't know GitHub yet, you should take a few days to learn the basics. You usually don't need more than the basics, and if you do, you can google the answer, but it's what virtually everyone is using these days for source code repositories. Some people will probably disagree with me because we all have different opinions. How are you learning? By studying Python on your own? Do you have a reasonable background in math? You might not need to use, say, algebra much unless you get into image processing or manipulation or gaming, but math really teaches you fundamentals of thinking logically. Anyway, if you have any more questions, feel free to hit me up. Always happy to help. It was easy for me, because I got a Commodore 64 when I was five and I've been programming since then, so the huge mountain of things to know was never that insurmountable since I've been doing it for 40 years, but one thing I will warn you about: if you want to be a developer, it has to be your career as well as your hobby, because if you don't spend some of your free time keeping up with tech changes, you'll fall behind. It's kind of exhausting and stressful that way.
@@vorpal22 Wow! You have such an elegant and meticulous way of writing!! I just have a single question. What should I do to be a full stack developer? What things should I learn?(not only languages but frameworks and stuff like that) I'm kinda new to programming. I have started with python before like 2 months and I think I know the basics. Does python really help me with being a full stack developer? Let me know your thoughts. Thanks!
I have some fun with itertools, but also I want to leave my answers. The firsts exercises of itertools section can be solved with: print(*lines[:5],sep=" ") print(*lines[1:5],sep=" ") print(*lines[1:5:2],sep=" ") The second with: [print(x,data[i+1])for i,x in enumerate(data[:-1])] And the last with: any(x
Any advantage of using islice in your example over using slicing notation like lines[:5] for speed, memory consumption? Since it's an iterable I suppose it's convenient only for iteration purposes only. In pairwise case is it possible to skip used letters?
(also mentioned this in detail in another answer) islice supports generators and you can't do regular slicing in generators because you can't index into a generator
on 4:00, I would make even a 2-ple with positional meaning into a class: @dataclass class Event: name: str duration: int def __str__(self): return self.name def __int__(self): return self.duration def __iter__(self): return iter(astuple(self)) # and just to be clever: def __add__(self, other): return sum(map(int, (self, other))) def __radd__(self, duration): return self.duration + duration events = (Event('learn', 5), Event('learn', 10), Event('relax', 20) Then my executable code is: minutes_studied = sum(event for event in events if 'learn' in event)
Regarding 6: What is the advantage of islice() instead of simply slicing the list using [start:end:step]? I guess the latter (and more widely used) is just syntactic sugar for islice()?
It is not the same thing. *lst[start:end:step]* is another list than *lst* that can be modified later separately form *lst* . It is created when you use that syntax, and each item belonging to the slicing will be copied into it. If the slice is huge, you will be consuming much memory and using much copying operations just by doing *lst[start:end:step]* But if you use *slc=islice(lst,start,end,step)* nothing will be copied or calculated. When you iterate through *slc* , python will retrieve values from the original list *lst* using the indices of that slicing. You can try it yourself. l1=[1,2,3,4] l2=l1[:] then modify *l1* by reassigning values, appending, popping etc... then iterate trough *l2* , it will contain the original 1, 2, 3, 4 However if you do like this, lst=[1,2,3,4] s=islice(lst,4) then modify *lst* then iterate trough *s* , you will find that it yields the modified values.
Thank you for another awesome video Patrick! I found your channel watching the Snake game video and yesterday I just posted a video of making my own version of the game. Thank you for your tutorials and giving me inspiration. 🙂
0:47 Note that sum() just calls the objects’ ﹎add﹎ method, so in principle you could use it for strings, for example. But the function actually goes to some lengths to forbid its use with strings. I think because concatenating a sequence of strings in this way is O(N²).
My personal tip for performance: Store everything as NumPy array and process it with Numba @njit optimized functions that are written as C-style for loops. And avoid allocation or at least allocate larger chunks at once. Good luck with optimizing your code and staying sane 😂
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.
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.
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
Unique features of your channel is Advanced Python course in your playlist. Which is not available in any channels.. My request to continue to do more on that topics
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.
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(...):
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
In "is this a bug or a feature in python"(shorts) you missed a comma after "three". Think Python therefor sums three and four as one? or ignoring it? But I might be wrong....
2:48, now if len(a) == len(b) is an actual code fact, I would assert it: assert len(a) == len(b), f'len(a)={len(a)} is not equal to len(b)={len(b)}' for da, db in zip(a, b):
@@sleeplessmidnight779 there are 2 main reasons why python is slower than C (and most other programming languages) (yes that fact that python is interpreted also slows it down but that is not one of the main reasons) Biggest Reason: it is dynamically-typed meaning that if you create a variable that is of a certain type you can easily change its type afterward (For example if you write a=5 (which is an integer) and then you write a="Hello" (which is a string) python will accept that instead of throwing an error like statically-typed languages) this means python will always need to check the type of the variable before any operation this issue can be overcome by using CPython where you can specify the types variables, CPython is not quite as fast as pure C but still a good option because its syntax is very similar to that of python's, and in fact you do not need to specify the type of every variable but the more you specify the faster the code will become 2nd Biggest Reason: it has a "Global Interpreter Lock" which means python can only use one of the threads/cores of your computer while executing code (this is for safety reasons and exists because Python is dynamically-typed) this issue can be overcome by using multithreading but using multithreading is less safe and you should only do it if you know what you are doing
There is no magical way to sum a list without enumerating it. However, not much can beat compiled C code, performance wise, which is what the built-in sum() function uses under-the-hood. It's generally at least 5x faster than a standard Python for loop.
So it's great, but in the real world where you have just average programmers of various ages, things like "lambda" will look like black magic to them. May be efficient but difficult to support software like that in the IT shop.
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
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.
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.
What you're looking at is just syntactic sugar. Recall that Python is a high level language which generally sits on top of C. Thus, it's all the same under-the-hood. Compile your Python to C and disassemble it. You'll see all of your familiar goto, cmp, jmp, etc. - statements.
1:00 I assume he means "enumerate" is more intuitive to use, since it avoids having to pass indices to arrays to retrieve values. I think he's right in this case. A lot of bugs live in for loops - especially near boundaries.
You know you are not avoiding loops right? under the hood it's still a loop. so even if you recommend using sum, you are getting around by using a buildin method, which still uses loops to acomplish the same result. This video is the definition of the stupidity going around about recommending stuff to people so they don't learn how things work. Yes, you are right, a personal made loop might be slower than the *super mega optimized built in method* but at least the poeple learn to do stuff on their own and not rely on everything being handed to them on a silver platter. Thanks for taking us into a sad future where people don't need to use brains.
It’s not only about run-time efficiency, but also code readability, maintainability and reduce the risk of introducing bugs. If you want to compute the sum of all values of a list, it is better to state exactly that on a single line that to create a custom loop, with local variables with no other purpose, and letting to the reader to identify the pattern.
There is a massive difference in performance between a loop of Python bytecode instructions (which a Python for loop will result in) and pure C loop which, for example, the sum() function will use.
@@juanignaciobruzzone2896 If your argument is that you want people to learn more; how can you argue against teaching people alternative (and often more efficient) ways to implement algoirthms beyond blindly repeating for loops all over the place?
On the other side, if you don't know the particular function (that you or someone else used) it will make the code less readable + and if you don't know all features of the functions you will get lost on finding a bug. Loops are not always bad if well designed inside a concept and using good commentary lines for descriptions/explanations.
I agree, but thanks Patrick, very useful to know.
The assumption is that you can read the manual to find out what things mean.
Problem here is more deep, than you might think. This guy said "functions are optimized", but that's not a complete true. Functions can allocate extra stuff during calculations what makes such ones not optimized. Allocations could be avoided using just loops.
Even if function is fully optimized you may have another flaw, because sometimes more than one operation is needed and then output from one function goes to input of another function, which now do extra calculations and may be not optimized.
This is why you should not rely on those functions on 100% if you don't understand concepts of memory management and optimization
@@alexmiller3260 well the easy answer here is that partially D'Oliviero already answered you and I can say that seems you are referring to iterative and recursive functions.
Of course if you are programming you have to know of how it works regarding memory management etc...
It is a choice you will make at the moment and maybe refactor it along the way...
For loops in Python are terribly slow, you should always avoid them.
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
)
Generators are great to save RAM memory for large arrays or Sequences. Although they can be slower than looping on array due to the need of computing each next step. My go to is choose the most readable by default and optimize if you need to.
As for islice, i largely prefer to use the slicing syntax in python which is simple, for exemple my_list[2:5:2] is equivalent of islice(my_list, 2, 5, 2).
It is not equivalent, the slicing syntax copy the slice while islice() is a generator that doesn't copy the array. For memory use and sometimes execution speed, islice will be better.
@@chaddaifouche536 Didn't know that, thanks
@@Migoyan Additionally, islice will also work on generators while the slicing syntax is specific to lists.
@Bebtelovimab I see you don't need tips on being unfunny.
@Bebtelovimab Ok bro, we understand you're a kid. Now you can resume your day.
Strange that you haven't mentioned, but when you want to use array elements as function arguments, you can simply write *array
(most useful example: print(*array) will print all of array elements separated by spaces (use argument sep='
' to write each value into new line) )
You can do that with dictionaries as well: **dict will place (key, value) pairs in the function call as optional arguments in a key=value form.
However this video is about for loops and not about arrays or other data structures, so I guess that's why function calls not mentioned.
2:25 zip() is also useful with database queries. Say you have a list or tuple of field names: you can use ", ".join(field_names) to format the field names for inclusion in a “select” statement; then call dict(zip()) on the returned tuple and the field names, to create a mapping from field names to field values.
You might want to look at DictCursor which is part of the python database API and does this for you.
@@glennmglazer I can’t see that in the standard library anywhere.
@@lawrencedoliveiro9104 he meant psycopg2 functionality. Don’t you use it to call db queries from your python code? I don’t believe you do it using standard library
@@baturin_m Not part of the “Python database API” that I can see. I have used APSW, pymysql and of course Oracle’s mysql.connector classes.
@@lawrencedoliveiro9104 got it. It’s strange that all of these connectors don’t provide this feature like psycopg2 does. It really helpes a lot in case you cannot use ORM for higher abstraction level.
best for loop → for (int i = 0; i < 10; i++)
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
Hi!
Regarding islice(), it's pretty enough, but the embedded list slicing is more faster and better, as for me:
lines[:5]
lines[1:5]
I was also thinking the same thing.
I think the main situation you would use islice is when you want to slice a generator. For a plain list, regular slicing in the index (as you've written) is easily understandable and also probably the most efficient. But generators don't support indexing and islice supports any iterable including generators. A small caveat that I discovered after checking the docs is that islice doesn't support negative indices but regular slicing does (e.g. last 3 elements lines[-3:])
islice() creates an iterator over the initial array content while list slicing creates a copy (with Python lists, slicing on numpy arrays just creates alias, which sometimes results in strange behaviour, probably the reason why basic Python doesn't do it this way).
On small slices this is not important but if you use big slices, in a nested loop, slicing may give a lot of work to your garbage collector.
A gem in a haystack!
Too bad you didn't include the timeit% numbers between the optimized vs unoptimized methods.
That would make the video perfect!
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.
3:38 note that, in the generator expression, you can use tuple unpacking as well.
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")
I am falling in love with Python and coding in general. It allows us to do so much with so little effort, except writing the code in the first place, that is hectic but once the code is ready and everything works you feel like a super human.
The more you do it, the more adept you will get at it, and when you write a truly elegant and performant piece of code (be it a function, a class, or whatever), it will feel like poetry. Same with elegant mathematical proofs.
I love Python, but I prefer Kotlin because it has so much elegant, built in functional programming constructs, and functional programming can take your code and shrink its length dramatically. Python has some functional programming concepts built in, but they're a bit clunky. It's still a great language, though, if you're not worried about performance (and if you are, just make sure you use built-ins and numpy, since they're implemented in C so they run much faster than loops).
@@vorpal22 Guess I gotta learn Kotlin next. But trying to get into coding, I feel like I should at least spend some time with Python more before learning another language. Although the job market requires multiple languages for us to land a job, I just feel like becoming advanced in one language could perhaps be beneficial. Any thoughts?
@@md.masumomarjashim I agree that you should focus on one language for awhile before moving to the next... if you really want to become a developer, you should have a strong working knowledge of at least one language, and then have some exposure to other languages so you cover your bases. You probably want to know some of each of the following:
1. Object-oriented programming
2. Procedural programming
3. Functional programming
Python is - according to many surveys - the language that is in highest demand now (followed by Java, but it is declining), so it's a good choice. There's a book called "Fluent Python" that is the best I have ever looked at: it is a hefty one, but you don't have to read it cover-to-cover and it'll teach you a lot of Python than you probably didn't even know existed. O'Reilly just updated it for Python 3.10, so it's very up-to-date, and you can just skip around the chapters and learn what you need or what interests you. No Python developer should be without it.
To learn Kotlin is a bit harder, because you do need to have some working knowledge of Java since Kotlin compiles into Java bytecode (same with Scala and Clojure, which are also both hybrid object-oriented and functional programming languages... Clojure is really messy, in my opinion, but it's interesting to dabble in because it makes you think about programming very differently).
Keep in mind that if you know a couple languages, many companies aren't too concerned about what languages you know: you can learn one pretty quickly and it's a small investment on their part. The best thing to do is show that you have problem solving skills. There is a website called "daily coding problem" that is really valuable and covers lots of questions you'd encounter in an interview. You can sign up for free to get the problems delivered to your inbox daily (they are ranked by easy, medium, and hard), and if you pay a small fee, you can also get the best solutions to the problems the next day.
Another thing that is becoming more and more popular in the hiring process is having a GitHub account with a bunch of repositories that you're working on: either personal projects, or contributions to open-source projects (I find that a bit more intimidating), school assignments, etc. It's like a programmer's equivalent of an artist's portfolio. If you don't know GitHub yet, you should take a few days to learn the basics. You usually don't need more than the basics, and if you do, you can google the answer, but it's what virtually everyone is using these days for source code repositories.
Some people will probably disagree with me because we all have different opinions. How are you learning? By studying Python on your own? Do you have a reasonable background in math? You might not need to use, say, algebra much unless you get into image processing or manipulation or gaming, but math really teaches you fundamentals of thinking logically.
Anyway, if you have any more questions, feel free to hit me up. Always happy to help. It was easy for me, because I got a Commodore 64 when I was five and I've been programming since then, so the huge mountain of things to know was never that insurmountable since I've been doing it for 40 years, but one thing I will warn you about: if you want to be a developer, it has to be your career as well as your hobby, because if you don't spend some of your free time keeping up with tech changes, you'll fall behind. It's kind of exhausting and stressful that way.
@@vorpal22 Wow! You have such an elegant and meticulous way of writing!! I just have a single question. What should I do to be a full stack developer? What things should I learn?(not only languages but frameworks and stuff like that) I'm kinda new to programming. I have started with python before like 2 months and I think I know the basics. Does python really help me with being a full stack developer? Let me know your thoughts. Thanks!
@@vorpal22 this is such a well thought and written comment. Trurly remarkable sir.
I have some fun with itertools, but also I want to leave my answers.
The firsts exercises of itertools section can be solved with:
print(*lines[:5],sep="
")
print(*lines[1:5],sep="
")
print(*lines[1:5:2],sep="
")
The second with:
[print(x,data[i+1])for i,x in enumerate(data[:-1])]
And the last with:
any(x
Any advantage of using islice in your example over using slicing notation like lines[:5] for speed, memory consumption? Since it's an iterable I suppose it's convenient only for iteration purposes only.
In pairwise case is it possible to skip used letters?
(also mentioned this in detail in another answer) islice supports generators and you can't do regular slicing in generators because you can't index into a generator
on 4:00, I would make even a 2-ple with positional meaning into a class:
@dataclass
class Event:
name: str
duration: int
def __str__(self): return self.name
def __int__(self): return self.duration
def __iter__(self): return iter(astuple(self))
# and just to be clever:
def __add__(self, other): return sum(map(int, (self, other)))
def __radd__(self, duration): return self.duration + duration
events = (Event('learn', 5), Event('learn', 10), Event('relax', 20)
Then my executable code is:
minutes_studied = sum(event for event in events if 'learn' in event)
Regarding 6:
What is the advantage of islice() instead of simply slicing the list using [start:end:step]?
I guess the latter (and more widely used) is just syntactic sugar for islice()?
It is not the same thing.
*lst[start:end:step]* is another list than *lst* that can be modified later separately form *lst* . It is created when you use that syntax, and each item belonging to the slicing will be copied into it. If the slice is huge, you will be consuming much memory and using much copying operations just by doing *lst[start:end:step]*
But if you use *slc=islice(lst,start,end,step)* nothing will be copied or calculated. When you iterate through *slc* , python will retrieve values from the original list *lst* using the indices of that slicing.
You can try it yourself.
l1=[1,2,3,4]
l2=l1[:]
then modify *l1* by reassigning values, appending, popping etc... then iterate trough *l2* , it will contain the original 1, 2, 3, 4
However if you do like this,
lst=[1,2,3,4]
s=islice(lst,4)
then modify *lst* then iterate trough *s* , you will find that it yields the modified values.
Suppose I write a generator which never terminates. Then I can use islice to extract any initial part of the infinite sequence.
@@lawrencedoliveiro9104 That's an amazing idea.
Python never ceases to amaze me. Loops being slow being be a not so subtle hint that python and production should never ever ever mix.
Very useful! I have always been writing inefficient and unreadble codes since I don't know these advanded methods.
Thank you for another awesome video Patrick! I found your channel watching the Snake game video and yesterday I just posted a video of making my own version of the game. Thank you for your tutorials and giving me inspiration. 🙂
Pairwise & takewhile were new to me. Thanks!!
0:47 Note that sum() just calls the objects’ ﹎add﹎ method, so in principle you could use it for strings, for example. But the function actually goes to some lengths to forbid its use with strings. I think because concatenating a sequence of strings in this way is O(N²).
python's strength is definetly not speed so i dont think it matters that much
this is a top tier content as a "cheat sheet" video. Thank you so much, Danke schön
My personal tip for performance: Store everything as NumPy array and process it with Numba @njit optimized functions that are written as C-style for loops. And avoid allocation or at least allocate larger chunks at once. Good luck with optimizing your code and staying sane 😂
Ward for the 1:45 tip to use later at a exam program later. Thanks, great video.
Did not know about the strict parameter added to zip in Py3.10. Nice!
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.
consider using math.fsum()
numpy is typically 3rd party, while math is built-in
Great video. Very useful. Thanks
Thank you Patrick.
Super cool man, I get more efficient every time you put out a video!
These are amazing tips. Thanks so much!
very interesting the itertools packages, i didnt know about it.
Useful tips, very good video!
Keep 'em coming my friend!
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.
Bad advise: "write more complex code, and another programmers will spend more and more time to understand your code".
Nice Video! Thanks a lot!!
Why use islice over indexing with a slice? Does it return a lazy generator instead of allocating a new sequence or something like that?
Short but so big Thanks.
Cool Tips, thanks for sharing, Patrick :)
Great videos so far! (this is my third video of yours today)
You really always share very valuable information.
Nice. Thank you.
In the zip example the equivalent would have been to do min(len(a),len(b).I still get that zip seems more convenient
love your videos man
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
Unique features of your channel is Advanced Python course in your playlist. Which is not available in any channels.. My request to continue to do more on that topics
Thanks for the feedback! Yes that's on my list for 2023
@@patloeber Thank you.
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.
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(...):
Thank you
The thumbnail is missing a bracket
You meant to do this instead:
for i in range(len(data)):
print(data[i])
What for should i use islice instead of [ : 5]?
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
Okay what is the vs code theme? Oh and good video, really helpful
one dark pro i guess
Which VS code theme you are using?
Thanks for the tips, but tell us, what's your VS Code theme?
In "is this a bug or a feature in python"(shorts) you missed a comma after "three". Think Python therefor sums three and four as one? or ignoring it? But I might be wrong....
Please tell me which plugin did you use in the design
What about recursion? p.s. It couldn’t be true that imperative language can't deal with loops or ? 🤔
Why use islice(lines, 5) instead of lines[:5] ?
Which ide ia this? Vscode?
Yes, VS Code w/ the "shades of purple" theme
isn’t an out of range array index supposed to return undefined?
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.
2:48, now if len(a) == len(b) is an actual code fact, I would assert it:
assert len(a) == len(b), f'len(a)={len(a)} is not equal to len(b)={len(b)}'
for da, db in zip(a, b):
You forgot range closing ')' in code in the thumbnail.
I’ll just keep it simple and use foreach(array $list as $key => &value); loop in php
If you had older RAM and CPU, would numpy's arange function be much slower? Or does the fact that it runs in C make it faster regardless?
It will almost always make it faster on the expense of more memory consumption and binary dependency on the import.
please write function code on cyber security and steganography
Can i use thi in for loops on Java? and, is it safety?
5:36 Itertools are in many cases slower than normal loops. I don't recommend them!
I also like shades of purple 💜
but bts, sum must be using for loop too, isn't it?
yes but it is implemented in C which is way faster
@@DendrocnideMoroides thanks bro, could you please explain why python is slow as compared to c, is it because python use interpreter ?
@@sleeplessmidnight779 there are 2 main reasons why python is slower than C (and most other programming languages) (yes that fact that python is interpreted also slows it down but that is not one of the main reasons)
Biggest Reason: it is dynamically-typed meaning that if you create a variable that is of a certain type you can easily change its type afterward (For example if you write a=5 (which is an integer) and then you write a="Hello" (which is a string) python will accept that instead of throwing an error like statically-typed languages) this means python will always need to check the type of the variable before any operation this issue can be overcome by using CPython where you can specify the types variables, CPython is not quite as fast as pure C but still a good option because its syntax is very similar to that of python's, and in fact you do not need to specify the type of every variable but the more you specify the faster the code will become
2nd Biggest Reason: it has a "Global Interpreter Lock" which means python can only use one of the threads/cores of your computer while executing code (this is for safety reasons and exists because Python is dynamically-typed) this issue can be overcome by using multithreading but using multithreading is less safe and you should only do it if you know what you are doing
There is no magical way to sum a list without enumerating it. However, not much can beat compiled C code, performance wise, which is what the built-in sum() function uses under-the-hood. It's generally at least 5x faster than a standard Python for loop.
Does anyone know the vscode theme he is using?
the thumbnail image is missing a ')' :P
What's your theme????
shades of purple
vs code theme name ?
Sir we want more python tips and tricks
Syntax error on line 1 in thumbnail 🤓
Also nice vid
I learned a lot from this vid; learned even more from the comments 🐍 💬
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
step 1: ask chatGPT
step 2: copy
step 3: paste
coding is one of the few activities where being lazy is actually helpful
Welp, may as well write my loop code in C/C++ and import it in python.
No you shouldn't. This is exactly what Python's built-ins do for you. Use them.
Some of these sound too confusing and harder too read
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
So it's great, but in the real world where you have just average programmers of various ages, things like "lambda" will look like black magic to them. May be efficient but difficult to support software like that in the IT shop.
Cool😊
Why is it always "for i " and Not "for anything " that can be :-D
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
What general purpose language(s) do you prefer?
@@djst3rling863 C#, rust, go, dart, kotlin, swift.
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.
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.
What you're looking at is just syntactic sugar.
Recall that Python is a high level language which generally sits on top of C. Thus, it's all the same under-the-hood.
Compile your Python to C and disassemble it. You'll see all of your familiar goto, cmp, jmp, etc. - statements.
“This is very error-prone”: why?
1:00 I assume he means "enumerate" is more intuitive to use, since it avoids having to pass indices to arrays to retrieve values. I think he's right in this case. A lot of bugs live in for loops - especially near boundaries.
7th tip: just use another language like C or Java
Anything is better than your thumbnail code because that doesn’t even run
😢
I am second
You know you are not avoiding loops right? under the hood it's still a loop. so even if you recommend using sum, you are getting around by using a buildin method, which still uses loops to acomplish the same result. This video is the definition of the stupidity going around about recommending stuff to people so they don't learn how things work. Yes, you are right, a personal made loop might be slower than the *super mega optimized built in method* but at least the poeple learn to do stuff on their own and not rely on everything being handed to them on a silver platter. Thanks for taking us into a sad future where people don't need to use brains.
It’s not only about run-time efficiency, but also code readability, maintainability and reduce the risk of introducing bugs.
If you want to compute the sum of all values of a list, it is better to state exactly that on a single line that to create a custom loop, with local variables with no other purpose, and letting to the reader to identify the pattern.
There is a massive difference in performance between a loop of Python bytecode instructions (which a Python for loop will result in) and pure C loop which, for example, the sum() function will use.
@@adrianbool4568 i agree with what you say, however, you are still missing my point.
@@juanignaciobruzzone2896 If your argument is that you want people to learn more; how can you argue against teaching people alternative (and often more efficient) ways to implement algoirthms beyond blindly repeating for loops all over the place?
Питон это жуткий ущербный язык.
Stick to the basics. Don't memorize useless stuffs.
clickbait
just dont use python if you care about performance