Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Generator Functions | Function Return Value Specification
Python Functions Tutorial
course content

Course Content

Python Functions Tutorial

Python Functions Tutorial

1. What is a Function in Python?
2. Positional and Optional Arguments
3. Arbitrary Arguments
4. Function Return Value Specification
5. Recursion and Lambda Functions

bookGenerator 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:

12345678910111213141516
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))
copy

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.

Task
test

Swipe to show code editor

Create a generator that generates unique identifiers. The identifiers can be, for example, strings in the format "ID_1", "ID_2", and so on. Each time you call next(), the generator should return a new unique identifier.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 4
toggle bottom row

bookGenerator 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:

12345678910111213141516
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))
copy

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.

Task
test

Swipe to show code editor

Create a generator that generates unique identifiers. The identifiers can be, for example, strings in the format "ID_1", "ID_2", and so on. Each time you call next(), the generator should return a new unique identifier.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 4
toggle bottom row

bookGenerator 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:

12345678910111213141516
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))
copy

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.

Task
test

Swipe to show code editor

Create a generator that generates unique identifiers. The identifiers can be, for example, strings in the format "ID_1", "ID_2", and so on. Each time you call next(), the generator should return a new unique identifier.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

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:

12345678910111213141516
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))
copy

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.

Task
test

Swipe to show code editor

Create a generator that generates unique identifiers. The identifiers can be, for example, strings in the format "ID_1", "ID_2", and so on. Each time you call next(), the generator should return a new unique identifier.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 4. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt