Thursday, October 7, 2021

Python for Beginners: Generators in Python

Do you know about functions in python? If you answered yes, let me take you through an interesting concept of generator functions and generators in python. In this article, we will look at how generators are defined and used in a program. We will also look at how a generator is different from a function using some examples.

What is a function?

In python, a function is a block of code that does some specific work. For example, a function can add two numbers, a function can delete a file from your computer, a function can do any specific task you want it to do. 

A function also returns the required output value using the return statement. An example of a function is given below. It takes two numbers as input, multiplies them and returns the output value using the return statement.

def multiplication(num1, num2):
    product = num1 * num2
    return product


result = multiplication(10, 15)
print("Product of 10 and 15 is:", result)

Output:

Product of 10 and 15 is: 150

What is a generator function?

A generator function is similar to a function in python but it gives an iterator-like generator to the caller as output  instead of an object or a value. Also, we use yield statements instead of return statements in a generator function. The yield statement pauses the execution of the generator function whenever it is executed and returns the output value to the caller. A generator function can have one or more than one yield statements but it cannot have a return statement.

We can define a generator function in a similar way to functions in python but we cannot use a return statement. Instead we use the yield statement. Following is an example of a generator function that returns numbers from 1 to 10 to the caller.

def num_generator():
    for i in range(1, 11):
        yield i


gen = num_generator()
print("Values obtained from generator function are:")
for element in gen:
    print(element)

Output:

Values obtained from generator function are:
1
2
3
4
5
6
7
8
9
10

What are generators in Python? 

Generators in python are a type of iterators that are used to execute generator functions using the next() function. To execute a generator function, we assign it to the generator variable. Then we use the next() method to execute the generator function.

The next() function takes the generator as input and executes the generator function till the next yield statement. After that, execution of the generator function is paused. To resume the execution, we again call the next() function with the generator as an input. Again, the generator function executes till the next yield statement. This process can be continued till the generator function’s execution gets finished. This process can be understood from the following example.

def num_generator():
    yield 1
    yield 2
    yield 3
    yield 4


gen = num_generator()
for i in range(4):
    print("Accessing element from generator.")
    element = next(gen)
    print(element)

Output:

Accessing element from generator.
1
Accessing element from generator.
2
Accessing element from generator.
3
Accessing element from generator.
4

Process finished with exit code 0

In the above output, you can observe that each time the next() function is called, the element from the next yield statement is printed. It shows that each time when the next() function is called, the generator function resumes its execution.

If we try to call the next() function with the generator as input after the generator function has finished its execution, the next() function raises StopIteration exception. So, It is advised to use the next() function inside a python try except block. Moreover, we can also iterate through the generators in python using the for loop. It will produce the same result as produced during the execution of the program using the next() function.

def num_generator():
    yield 1
    yield 2
    yield 3
    yield 4


gen = num_generator()
for i in gen:
    print("Accessing element from generator.")
    print(i)

Output:

Accessing element from generator.
1
Accessing element from generator.
2
Accessing element from generator.
3
Accessing element from generator.
4

Process finished with exit code 0

Examples of generators in Python

As we have discussed generators and generator functions in Python, Let us implement a program to understand the above concepts in a better way. In the following program, we implement a generator function that takes a list as input and calculates the square of elements in the list.

myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


def square_generator(input_list):
    for element in input_list:
        print("Returning the square of next element:",element)
        yield element*element


print("The input list is:",myList)
gen = square_generator(myList)
for i in range(10):
    print("Accessing square of next element from generator.")
    square = next(gen)
    print(square)

Output:

The input list is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Accessing square of next element from generator.
Returning the square of next element: 1
1
Accessing square of next element from generator.
Returning the square of next element: 2
4
Accessing square of next element from generator.
Returning the square of next element: 3
9
Accessing square of next element from generator.
Returning the square of next element: 4
16
Accessing square of next element from generator.
Returning the square of next element: 5
25
Accessing square of next element from generator.
Returning the square of next element: 6
36
Accessing square of next element from generator.
Returning the square of next element: 7
49
Accessing square of next element from generator.
Returning the square of next element: 8
64
Accessing square of next element from generator.
Returning the square of next element: 9
81
Accessing square of next element from generator.
Returning the square of next element: 10
100

In the above example, you can see that whenever the next() function is executed with the generator as input, It executes the loop one time till the yield statement. Once the yield statement is executed, the execution of the generator function is paused until we execute the next() function again.

Main difference between a function and a generator function

The main differences between a function and a generator function are as follows.

  • A function has a return statement while a generator function has a yield statement.
  • A function stops its execution after execution of the first return statement. Whereas, a generator function just pauses the execution after execution of the yield statement. 
  • A function returns a value or a container object while a generator function returns a generator object.

Conclusion

In this article, we have discussed generator functions and generators in Python.To learn more about python programming, you can read this article on list comprehension. You may also like this article on the linked list in Python.

The post Generators in Python appeared first on PythonForBeginners.com.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...