Generators:
In this thread we are going to discuss generators in python with examples..
A function that can be used to control the iteration behavior of a loop. A generator is very similar to a function that returns a list. Generators are a new form of syntax introduced in python version 3. It is a much nicer way to create an iterator. The generator syntax is quite simple. Let’s create our generator.
Code:
def gen(n):
for i in range(n):
yield i
for i in gen(5):
print(i)
So now you can see we created a ‘gen’ function which is generating numbers from 0 to n. But you are thinking about what that yield statement is. The ‘yield’ function is similar to the return function but there is a slight difference when the yield function is called unlike the return function which returns a value and terminates the function. The yield function pauses the execution of the function and returns the value whatever is iterating through the generator. Yield does not terminate the function execution, it just pauses it. And start it again after returning the value.
We can use the next() function to manually iterate through a generator. Let’s code it for better understanding.
Code:
def gen(n):
for i in range(n):
yield i
x = gen(7)
print(next(x))
print(next(x))
print(next(x))
print(next(x))
In the for loop the next function is automatically called. Now if you have any doubts about generators the example i am giving below will probably fix it for you
Code:
def gen():
yield 1
print("print 1 and pause")
yield 2
print("print 2 and pause")
yield 3
print("print 3 and pause")
yield 4
print("print 4 and pause")
x = gen()
print(next(x))
print(next(x))
print(next(x))
print(next(x))
We are passing no arguments we are just calling yield function and first it will return us 1 and stops the execution of function and the code with next function runs after that execution will again transfers to the function and it will generate a value and yield it and so on the process repeats it is not storing values inside memory it is just generating them and printing them.
We use generators when we don’t care about the before and after case. We just care about the current case. We use it to generate the sequence of numbers. And when we are looping through some data structures or something where we don’t care about before and after the current element, we just care about the current item in that case we use the Generators. We can’t use generators when we care about cases before and after then we can’t use generators because in generators we don’t care about them.
Program to make a generator for prime numbers between a range
Code:
################## Prime Numbers Generator ##############
def isPrime(x):
if x < 2:
return False
elif x == 2:
return True
for n in range(2, x):
if x % n ==0:
return False
return True
def primeGenerator(a, b):
#your code goes here
for i in range(a, b):
if isPrime (i):
yield i
f = int(input())
t = int(input())
print(list(primeGenerator(f, t)))
So that’s it for now till then.
"Keep Good care of your Health
Allah Hafiz and Good Bye Till the next post"
"Learn for a better future"
Love me like you do