Pre-requisites for Generators:
=========================================================================
In order to grasp the concept of generators in Python, it is essential to be familiar with the following concepts: Iterable, Iterator, and Iteration. Let's explore each of these concepts individually.
Iterable:
An Iterable is a Python object that has either the iter() or getitem() method defined, allowing it to provide an Iterator. Not all data types are iterable. For example, integers are not iterable, but they can be converted to strings for iteration purposes. Common iterable data types include strings, lists, tuples, and dictionaries.
Iterator:
An Iterator is a value within an Iterable that has the next() method defined. The Iterator retrieves values based on the conditions defined for it.
Iteration:
Iteration refers to the process of obtaining Iterators from an Iterable.
Let's illustrate these concepts using the following code:
we will get values from 0 to 8, here in range __iter__() and __next__() both method were defined. How ???? lets go line by line initially the value is one and we get it using __iter__() method and then we get 1,2,3,4,5,6 using __iter__() method but each time range method add 1 in value hence the value of i continuously increasing till we met condition.
Generators in Python: Simplifying Iteration
=========================================================================
Generators in python simply perform above mentioned work without using any method which we have seen above in definition of iterable and iterator which are __iter__() and __next__()
In other words generators are the functions which return an object (Iterator) which perform iteration over iterable over period of time.
Difference between normal function and Generator Function:
1) In Generator Function we use yield keyword instead of return in normal function.
2) Generator function produce one result at a time where as in normal function we get entire result.
Examples:
1 ) Fibonacci series:
in above code i have used for loop to get values one by one, if i missed to use for loop each time i need to run next() to get new value in Fibonacci series.
2) Even values in given range:
In the above example i have used Generator expression instead of yield keyword, if you print value from above code you will get <generator object <genexpr> at (Memory location)> in return.
The range function itself gives me step and start value. range(start : end: step) when i start printing it will start from first value i have entered which is 2 and it will continue printing till 19 with step of 2. Why19 ?? in range function end value is excluded.
For Odd value you can make change in range function, instead of 2 start range function from 1.
Advantages:
1) Produce infinite item
2)Easy to implement , no need of using __iter__ or __next__ everytime
3) Can be use with loops,it makes operation more easy
Conclusion:
Generators in Python offer a powerful and memory-efficient approach to iteration. By understanding the concepts of Iterable, Iterator, and Iteration, and leveraging the differences between normal functions and generator functions, you can create efficient code that produces results over time without