Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 2.8: Generators with Example

2 Replies, 1397 Views

[Image: BFMMlbcQvml9HSqXcvNp]


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 Heart
(This post was last modified: 05-18-2022, 04:15 PM by Covid-19.)
Amazing and clear.. BTW this function is really useful... I can make a plently of things done with it.
Rs
* Thankful to Allah *
Kurdy
(05-20-2022, 01:06 PM)Mr.Kurd Wrote: Amazing and clear.. BTW this function is really useful... I can make a plently of things done with it.
Thank you Kurd  Heart
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,249 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,639 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,675 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,844 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,609 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,844 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,793 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,833 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,826 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,828 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)