Day #91 - Generators in Python

Day #91 - Generators in Python

Introduction

Welcome to my 91st blog post on the Python journey. On day 91, I learned about a special type of function called Generators that allows us to create iterable sequences of values. They help us to generate on-the-fly values. Let's dive into more details and understand Generators in Python.

So let's get started......

Generators

  • They allow us to create an iterable sequence of values.

  • It returns a generator object, that can be used to generate the values one-by-one as you iterate over it i.e we can generate values on-the-fly with generators.

  • They act as a powerful tool for working with large or complex data sets.

  • Using generators can help us to move away from storing data in memory as the values are generated on spot.

Create Generator

  • We can create a generator by using the 'yield' statement in a function.

  • The yield statement returns a value from the generator and suspends the execution of the function until the next value is requested.

Example -

As can be seen in the below example, the function my_generator() is a generator function that returns a generator object, which can be used to generate values in the range 0 to 4. The next() function is used to request the next value from the generator.

def my_generator():
    for i in range(5):
        yield i

gen = my_generator()
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
# Output:
# 0
# 1
# 2
# 3
# 4

Use Generator

We can use generators in a variety of ways, such as in a for loop, a list comprehension, or a generator expression.

gen = my_generator()
for i in gen:
    print(i)
# Output:
# 0
# 1
# 2
# 3
# 4

Benefits of Generators

Generators offer many benefits over other sequence data types like lists, tuples, and sets. Below are listed some of the major benefits of Generators

  • They allow us to generate the values of the sequence on-the-fly, rather than creating and storing them in memory just like lists

  • Using generators we can easily work on large or complex datatypes.

  • Generators are lazy, meaning the values are generated only when they are requested.

Resources Used

You can watch the video of Day#91 by clicking on the below link 👇👇👇👇👇

Conclusion

Thanks, guys for going through this blog post. On day 91, I learned about the generators in Python that can be useful to generate sequence values on the go. This help in using memory efficiently and avoid storage of data, unlike other iterable objects like lists.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #91

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.