From b06cfac409773c137029aa307f6cd792b152b5c7 Mon Sep 17 00:00:00 2001 From: Matteo Masia Date: Mon, 13 Oct 2025 12:10:48 +0200 Subject: [PATCH 1/9] add a new entry about the term yield --- .../concepts/keywords/terms/yield/yield.md | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 content/python/concepts/keywords/terms/yield/yield.md diff --git a/content/python/concepts/keywords/terms/yield/yield.md b/content/python/concepts/keywords/terms/yield/yield.md new file mode 100644 index 00000000000..37aa161a2ef --- /dev/null +++ b/content/python/concepts/keywords/terms/yield/yield.md @@ -0,0 +1,116 @@ +--- +Title: 'Yield' +Description: 'Returns an iterator and pauses execution of a generator function.' +Subjects: + - 'Code Foundations' + - 'Computer Science' + - 'Interview Prep' +Tags: + - 'Functions' + - 'Iterators' + - 'Python' +CatalogContent: + - 'learn-intermediate-python-3-iterators-and-generators' + - 'learn-intermediate-python-3' +--- + +The **`yield`** keyword is used in a [function](https://www.codecademy.com/resources/docs/python/functions) to make it a [generator function](https://www.codecademy.com/resources/docs/python/generators). +[Generator functions](https://www.codecademy.com/resources/docs/python/generators) return an [iterator](https://www.codecademy.com/resources/docs/python/iterators) that _produces one value per function call_, rather than _all values at once from the function called_. +This is possible because the [function](https://www.codecademy.com/resources/docs/python/functions) _maintains its own state_, and with each call it resumes from the previous state and performs the next call. +This behavior enhances memory efficiency when working with large datasets, avoiding the need to store all values in memory at once. It also allows for better resource management, such as connecting to a database. + +## Syntax + +```pseudo +def function_name(parameters): + # ... + yield value + # ... +``` + + +## Example + +The following example shows how the yield expression is used within a generator function: +```py +def read_huge_file(file_name): + + with open(file_name) as file: + for line in file: + yield line +``` +The code above returns an iterator that contains all the lines of an input file passed as an argument. +On each iteration, `yield` will return a single line read from the file. + + +## Codebyte Example: Printing a list of words with `return` vs. `yield` + +This Codebyte example shows the difference between using `return` and `yield` in a function. + +```codebyte/python +# A list of words to print +words: [str] = ["Hello", "yield", "!"] + +# A function that uses return, with print statements before and after +def print_list_by_return(element: str): + print("Before return") + return element + print("After return") + +# A function that uses yield, with print statements before and after +def print_list_by_yield(element: str): + print("Before yield") + yield element + print("After yield") + +# Calling both functions and printing their outputs +print("calling print_list_by_return:\n") +for word in words: + print(print_list_by_return(word)) + +print(f"\n{'#'*20}\n") + +print("calling print_list_by_yield:\n") +for word in words: + yield_obj = print_list_by_yield(word) + for value in yield_obj: + print(value) +``` + +> **Note:** Because `yield` produces an iterator, we need to loop through the iterator to get the values to print. +> If not, we'll see the generator object memory address printed. + +The output of the code above will be: + +``` +calling print_list_by_return: + +Before return +Hello +Before return +yield +Before return +! + +#################### + +calling print_list_by_yield: + +Before yield +Hello +After yield +Before yield +yield +After yield +Before yield +! +After yield +``` + +As we can see from the output above: + +* The function with `return` only prints the statement `"Before return"`. The statement `"After return"` is never printed, + because the function exits after the `return`. +* The function with `yield` prints both statements `"Before yield"` and `"After yield"` for each word in the list. + This is because the function pauses at the `yield` statement and resumes after it when the next value is requested from the iterator. + From 85d626d0d6d6d4edd466a529acc0a84995147d87 Mon Sep 17 00:00:00 2001 From: Matteo Masia Date: Mon, 13 Oct 2025 15:55:18 +0200 Subject: [PATCH 2/9] fix linting issues --- .../python/concepts/keywords/terms/yield/yield.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/content/python/concepts/keywords/terms/yield/yield.md b/content/python/concepts/keywords/terms/yield/yield.md index 37aa161a2ef..807e3d6ea9a 100644 --- a/content/python/concepts/keywords/terms/yield/yield.md +++ b/content/python/concepts/keywords/terms/yield/yield.md @@ -28,21 +28,21 @@ def function_name(parameters): # ... ``` - ## Example The following example shows how the yield expression is used within a generator function: + ```py def read_huge_file(file_name): - + with open(file_name) as file: for line in file: yield line ``` + The code above returns an iterator that contains all the lines of an input file passed as an argument. On each iteration, `yield` will return a single line read from the file. - ## Codebyte Example: Printing a list of words with `return` vs. `yield` This Codebyte example shows the difference between using `return` and `yield` in a function. @@ -70,7 +70,7 @@ for word in words: print(f"\n{'#'*20}\n") -print("calling print_list_by_yield:\n") +print("calling print_list_by_yield:\n") for word in words: yield_obj = print_list_by_yield(word) for value in yield_obj: @@ -109,8 +109,7 @@ After yield As we can see from the output above: -* The function with `return` only prints the statement `"Before return"`. The statement `"After return"` is never printed, +- The function with `return` only prints the statement `"Before return"`. The statement `"After return"` is never printed, because the function exits after the `return`. -* The function with `yield` prints both statements `"Before yield"` and `"After yield"` for each word in the list. +- The function with `yield` prints both statements `"Before yield"` and `"After yield"` for each word in the list. This is because the function pauses at the `yield` statement and resumes after it when the next value is requested from the iterator. - From 6b96a58debbba08427ecb27fbc1416ffcb599c79 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 14 Oct 2025 17:17:22 +0530 Subject: [PATCH 3/9] code example fix --- .../concepts/keywords/terms/yield/yield.md | 108 +++++++----------- 1 file changed, 42 insertions(+), 66 deletions(-) diff --git a/content/python/concepts/keywords/terms/yield/yield.md b/content/python/concepts/keywords/terms/yield/yield.md index 807e3d6ea9a..2be9f64c16b 100644 --- a/content/python/concepts/keywords/terms/yield/yield.md +++ b/content/python/concepts/keywords/terms/yield/yield.md @@ -1,115 +1,91 @@ --- Title: 'Yield' -Description: 'Returns an iterator and pauses execution of a generator function.' +Description: 'Turns a function into a generator, producing values one at a time while retaining state between calls.' Subjects: - 'Code Foundations' - 'Computer Science' - - 'Interview Prep' Tags: - 'Functions' - 'Iterators' - 'Python' CatalogContent: - - 'learn-intermediate-python-3-iterators-and-generators' - - 'learn-intermediate-python-3' + - 'learn-python-3' + - 'paths/computer-science' --- -The **`yield`** keyword is used in a [function](https://www.codecademy.com/resources/docs/python/functions) to make it a [generator function](https://www.codecademy.com/resources/docs/python/generators). -[Generator functions](https://www.codecademy.com/resources/docs/python/generators) return an [iterator](https://www.codecademy.com/resources/docs/python/iterators) that _produces one value per function call_, rather than _all values at once from the function called_. -This is possible because the [function](https://www.codecademy.com/resources/docs/python/functions) _maintains its own state_, and with each call it resumes from the previous state and performs the next call. -This behavior enhances memory efficiency when working with large datasets, avoiding the need to store all values in memory at once. It also allows for better resource management, such as connecting to a database. +The **`yield`** keyword is used in a [function](https://www.codecademy.com/resources/docs/python/functions) to make it a [generator function](https://www.codecademy.com/resources/docs/python/generators). Generator functions return an [iterator](https://www.codecademy.com/resources/docs/python/iterators) that produces one value per call instead of all values at once. The function maintains its state between calls, resuming from where it left off. This enhances memory efficiency for large datasets and allows better resource management, such as processing streams or database results. ## Syntax ```pseudo -def function_name(parameters): +def generatorFunction(): # ... yield value # ... ``` +**Parameters:** + +- `value`: The object to be produced by the generator. If omitted, `None` is yielded. + +**Return value:** + +`yield` returns the next value in a generator sequence to the caller. + ## Example -The following example shows how the yield expression is used within a generator function: +In this example, the generator yields numbers from 1 to 5, producing values one at a time: ```py -def read_huge_file(file_name): +def count_up_to_five(): + for i in range(1, 6): + yield i - with open(file_name) as file: - for line in file: - yield line -``` +# Create the generator +gen = count_up_to_five() -The code above returns an iterator that contains all the lines of an input file passed as an argument. -On each iteration, `yield` will return a single line read from the file. +# Iterate over the generator to get values +for number in gen: + print(number) +``` ## Codebyte Example: Printing a list of words with `return` vs. `yield` -This Codebyte example shows the difference between using `return` and `yield` in a function. +This Codebyte example shows the difference between using `return` and `yield` in a function: ```codebyte/python # A list of words to print -words: [str] = ["Hello", "yield", "!"] +words: list[str] = ["Hello", "yield", "!"] -# A function that uses return, with print statements before and after +# Function using return def print_list_by_return(element: str): - print("Before return") - return element - print("After return") + print("Before return") + return element + print("After return") # never executes -# A function that uses yield, with print statements before and after +# Function using yield def print_list_by_yield(element: str): - print("Before yield") - yield element - print("After yield") + print("Before yield") + yield element + print("After yield") # executes when generator resumes -# Calling both functions and printing their outputs +# Calling functions print("calling print_list_by_return:\n") for word in words: - print(print_list_by_return(word)) + print(print_list_by_return(word)) print(f"\n{'#'*20}\n") print("calling print_list_by_yield:\n") for word in words: - yield_obj = print_list_by_yield(word) - for value in yield_obj: - print(value) + yield_obj = print_list_by_yield(word) + for value in yield_obj: + print(value) ``` -> **Note:** Because `yield` produces an iterator, we need to loop through the iterator to get the values to print. -> If not, we'll see the generator object memory address printed. - -The output of the code above will be: - -``` -calling print_list_by_return: - -Before return -Hello -Before return -yield -Before return -! - -#################### - -calling print_list_by_yield: - -Before yield -Hello -After yield -Before yield -yield -After yield -Before yield -! -After yield -``` +> **Note:** Because `yield` produces an iterator, we need to loop through the iterator to get the values to print. If not, we'll see the generator object memory address printed. -As we can see from the output above: +In this code: -- The function with `return` only prints the statement `"Before return"`. The statement `"After return"` is never printed, - because the function exits after the `return`. -- The function with `yield` prints both statements `"Before yield"` and `"After yield"` for each word in the list. - This is because the function pauses at the `yield` statement and resumes after it when the next value is requested from the iterator. +- The `return` function prints only `"Before return"` because it exits immediately. +- The `yield` function prints `"Before yield"` and `"After yield"` for each item, pausing at `yield` and resuming when iterated. From d15a68512a48a22c40e6c3e64006d67c5360ef7b Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 14 Oct 2025 17:17:39 +0530 Subject: [PATCH 4/9] Update yield.md --- content/python/concepts/keywords/terms/yield/yield.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/keywords/terms/yield/yield.md b/content/python/concepts/keywords/terms/yield/yield.md index 2be9f64c16b..b2bf708944e 100644 --- a/content/python/concepts/keywords/terms/yield/yield.md +++ b/content/python/concepts/keywords/terms/yield/yield.md @@ -1,5 +1,5 @@ --- -Title: 'Yield' +Title: 'yield' Description: 'Turns a function into a generator, producing values one at a time while retaining state between calls.' Subjects: - 'Code Foundations' From 022285843380bf9094097c9be7e449a1a71e1898 Mon Sep 17 00:00:00 2001 From: Matteo Masia Date: Wed, 15 Oct 2025 09:08:47 +0200 Subject: [PATCH 5/9] Title: switch back to Capitalized Definition: added information on yield usage Example: add output and example explanation Codebyte: change function references on code explanation --- .../concepts/keywords/terms/yield/yield.md | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/content/python/concepts/keywords/terms/yield/yield.md b/content/python/concepts/keywords/terms/yield/yield.md index b2bf708944e..68f327ae5e3 100644 --- a/content/python/concepts/keywords/terms/yield/yield.md +++ b/content/python/concepts/keywords/terms/yield/yield.md @@ -1,5 +1,5 @@ --- -Title: 'yield' +Title: 'Yield' Description: 'Turns a function into a generator, producing values one at a time while retaining state between calls.' Subjects: - 'Code Foundations' @@ -13,7 +13,7 @@ CatalogContent: - 'paths/computer-science' --- -The **`yield`** keyword is used in a [function](https://www.codecademy.com/resources/docs/python/functions) to make it a [generator function](https://www.codecademy.com/resources/docs/python/generators). Generator functions return an [iterator](https://www.codecademy.com/resources/docs/python/iterators) that produces one value per call instead of all values at once. The function maintains its state between calls, resuming from where it left off. This enhances memory efficiency for large datasets and allows better resource management, such as processing streams or database results. +The **`yield`** keyword is used in a [function](https://www.codecademy.com/resources/docs/python/functions) to make it a [generator function](https://www.codecademy.com/resources/docs/python/generators). Generator functions return an [iterator](https://www.codecademy.com/resources/docs/python/iterators) that produces one value per call instead of all values at once. To get a value from a generator, call the `next()` function on it. The code within the function is executed up to the `yield` statement. When `yield` is encountered, the current value is returned. The function maintains its state between calls, resuming from where it left off. This enhances memory efficiency for large datasets and allows better resource management, such as processing streams or database results. ## Syntax @@ -37,6 +37,7 @@ def generatorFunction(): In this example, the generator yields numbers from 1 to 5, producing values one at a time: ```py +# Define a generator function that yields numbers from 1 to 5 def count_up_to_five(): for i in range(1, 6): yield i @@ -49,6 +50,26 @@ for number in gen: print(number) ``` +The output of this code is: + +``` +1 +2 +3 +4 +5 +``` + +> **Note:** `for` loop implicitly calls `next()` method on the generator `gen`. + +In this code: + +- `count_up_to_five` is defined as a generator function. +- `count_up_to_five()` is called and it returns a generator object without executing the function body. +- `for` loop calls `next()` on `gen`, which starts executing `count_up_to_five`. +- `count_up_to_five` runs until it hits the `yield` statement, returning the current value of `i`. +- `for` loop calls `next()` again, `count_up_to_five` function resumes right after the `yield` statement. + ## Codebyte Example: Printing a list of words with `return` vs. `yield` This Codebyte example shows the difference between using `return` and `yield` in a function: @@ -87,5 +108,5 @@ for word in words: In this code: -- The `return` function prints only `"Before return"` because it exits immediately. -- The `yield` function prints `"Before yield"` and `"After yield"` for each item, pausing at `yield` and resuming when iterated. +- The `print_list_by_return` function prints only `"Before return"` because it exits immediately. +- The `print_list_by_yield` function prints `"Before yield"` and `"After yield"` for each item, pausing at `yield` and resuming when iterated. From 4e2aa95e25dd789306e0e017f2f7a02ce3e6f14d Mon Sep 17 00:00:00 2001 From: Radhika-okhade Date: Wed, 22 Oct 2025 14:25:00 +0530 Subject: [PATCH 6/9] Update content/python/concepts/keywords/terms/yield/yield.md --- content/python/concepts/keywords/terms/yield/yield.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/keywords/terms/yield/yield.md b/content/python/concepts/keywords/terms/yield/yield.md index 68f327ae5e3..425f66702e1 100644 --- a/content/python/concepts/keywords/terms/yield/yield.md +++ b/content/python/concepts/keywords/terms/yield/yield.md @@ -1,5 +1,5 @@ --- -Title: 'Yield' +Title: 'yield' Description: 'Turns a function into a generator, producing values one at a time while retaining state between calls.' Subjects: - 'Code Foundations' From 0651cd469ffdb2ea97ef5be6794ec64d88682c4f Mon Sep 17 00:00:00 2001 From: Radhika-okhade Date: Wed, 22 Oct 2025 14:28:18 +0530 Subject: [PATCH 7/9] Update content/python/concepts/keywords/terms/yield/yield.md --- content/python/concepts/keywords/terms/yield/yield.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/keywords/terms/yield/yield.md b/content/python/concepts/keywords/terms/yield/yield.md index 425f66702e1..8befb7ea8b1 100644 --- a/content/python/concepts/keywords/terms/yield/yield.md +++ b/content/python/concepts/keywords/terms/yield/yield.md @@ -52,7 +52,7 @@ for number in gen: The output of this code is: -``` +```shell 1 2 3 From 01b5b100e79229997e522aaa0f342dbe77e4d004 Mon Sep 17 00:00:00 2001 From: Radhika-okhade Date: Wed, 22 Oct 2025 14:42:49 +0530 Subject: [PATCH 8/9] Update content/python/concepts/keywords/terms/yield/yield.md --- content/python/concepts/keywords/terms/yield/yield.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/content/python/concepts/keywords/terms/yield/yield.md b/content/python/concepts/keywords/terms/yield/yield.md index 8befb7ea8b1..0f1b0e6006b 100644 --- a/content/python/concepts/keywords/terms/yield/yield.md +++ b/content/python/concepts/keywords/terms/yield/yield.md @@ -75,6 +75,8 @@ In this code: This Codebyte example shows the difference between using `return` and `yield` in a function: ```codebyte/python +from typing import List + # A list of words to print words: list[str] = ["Hello", "yield", "!"] From e9d311fa4518b19b8ffdce01372ed9b67bb33391 Mon Sep 17 00:00:00 2001 From: Radhika-okhade Date: Wed, 22 Oct 2025 14:42:57 +0530 Subject: [PATCH 9/9] Update content/python/concepts/keywords/terms/yield/yield.md --- content/python/concepts/keywords/terms/yield/yield.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/keywords/terms/yield/yield.md b/content/python/concepts/keywords/terms/yield/yield.md index 0f1b0e6006b..bdaa52506b2 100644 --- a/content/python/concepts/keywords/terms/yield/yield.md +++ b/content/python/concepts/keywords/terms/yield/yield.md @@ -78,7 +78,7 @@ This Codebyte example shows the difference between using `return` and `yield` in from typing import List # A list of words to print -words: list[str] = ["Hello", "yield", "!"] +words: List[str] = ["Hello", "yield", "!"] # Function using return def print_list_by_return(element: str):