Course Content
Python Functions Tutorial
Python Functions Tutorial
Generator Functions
A generator function is a special type of function that uses the yield
keyword instead of return
to generate a sequence of values. When a generator function is called, it returns an iterator object, which can be iterated over to retrieve values one at a time.
The main advantage of generator functions is their memory efficiency. Generator functions generate values on-the-fly as they are needed, rather than generating the entire sequence upfront. This makes them memory efficient, especially when dealing with large datasets or infinite sequences.
Let's look at the example of the simple generator. This function is a generator that yields logins one by one from the given list:
def unique_logins_from_list(login_list): # Iterate over each login in the list for login in login_list: yield login # `yield` the current login # A predefined list of available logins login_list = ["user1", "user2", "user3", "user4", "user5"] # Creating a generator instance from the login list login_generator = unique_logins_from_list(login_list) # Generate and print 5 logins, one at a time for _ in range(5): # Each call to `next()` gives the next login print(next(login_generator))
The principle of a generator is that it allows values to be returned one at a time using the yield
keyword, without storing them all in memory at once. In our example, the unique_logins_from_list
generator iterates through the list of logins, returning each one on yield
and pausing at that point. When next()
is called, the generator resumes from where it left off, efficiently yielding values without needing to store the entire list in memory simultaneously. This makes generators particularly useful for handling large data sets or streams of data.
Swipe to start coding
Generate unique user IDs using a generator function. The id_generator()
function should continuously produce identifiers like "ID_1"
, "ID_2"
, etc.
- Initialize the variable
count
with the value 1, as identifiers start from 1. - Use an infinite
while
loop to continuously generate identifiers. - Use
yield
to return the current identifier in the formatf"ID_{count}"
. - Increment
count
by 1 after each iteration. - Initialize the generator object
id_gen
by callingid_generator()
.
Solution
Thanks for your feedback!
Generator Functions
A generator function is a special type of function that uses the yield
keyword instead of return
to generate a sequence of values. When a generator function is called, it returns an iterator object, which can be iterated over to retrieve values one at a time.
The main advantage of generator functions is their memory efficiency. Generator functions generate values on-the-fly as they are needed, rather than generating the entire sequence upfront. This makes them memory efficient, especially when dealing with large datasets or infinite sequences.
Let's look at the example of the simple generator. This function is a generator that yields logins one by one from the given list:
def unique_logins_from_list(login_list): # Iterate over each login in the list for login in login_list: yield login # `yield` the current login # A predefined list of available logins login_list = ["user1", "user2", "user3", "user4", "user5"] # Creating a generator instance from the login list login_generator = unique_logins_from_list(login_list) # Generate and print 5 logins, one at a time for _ in range(5): # Each call to `next()` gives the next login print(next(login_generator))
The principle of a generator is that it allows values to be returned one at a time using the yield
keyword, without storing them all in memory at once. In our example, the unique_logins_from_list
generator iterates through the list of logins, returning each one on yield
and pausing at that point. When next()
is called, the generator resumes from where it left off, efficiently yielding values without needing to store the entire list in memory simultaneously. This makes generators particularly useful for handling large data sets or streams of data.
Swipe to start coding
Generate unique user IDs using a generator function. The id_generator()
function should continuously produce identifiers like "ID_1"
, "ID_2"
, etc.
- Initialize the variable
count
with the value 1, as identifiers start from 1. - Use an infinite
while
loop to continuously generate identifiers. - Use
yield
to return the current identifier in the formatf"ID_{count}"
. - Increment
count
by 1 after each iteration. - Initialize the generator object
id_gen
by callingid_generator()
.
Solution
Thanks for your feedback!