Both regular functions and generators are there for the creation of iterators in Python for Data Science. Generators are more powerful and convenient for the creation of iterators. It also offers evaluation of elements on demand. In generators yield statement is there, which saves the state of the function from the previous call and can pick up or resume next time generator function is run. Also, memory consumption is less in the case of generator functions.
The functions _iter_() and _next_() make the generator function a compact and reliable function.
Generator functions vs. Normal functions:
- Generator function consists of one or many yield statements in place of return statements.
- A generator function can remember the local variables and the states they were in between successive calls. But this is not possible in case of normal functions.
- StopIteration is run automatically as the function terminates. This is not the case in normal function.
- In case of generator function, when the function yields then the function is stop and the control goes to the caller.
A generator can be written without the yield function, but this is not as powerful as the one with the yield function.
A list is there using the generator expression,
If you want to learn more about generators in python for data science, then you can check this and this as well. To be a successful data scientist you must learn different languages, tools and concepts. And then their implimentation on real time projects then only you can look for a career in it.