Example: Sending values to a generator - 3.6.1 | Chapter 3: Generators and Iterators | Python Advance
K12 Students

Academics

AI-Powered learning for Grades 8–12, aligned with major Indian and international curricula.

Academics
Professionals

Professional Courses

Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.

Professional Courses
Games

Interactive Games

Fun, engaging games to boost memory, math fluency, typing speed, and English skillsβ€”perfect for learners of all ages.

games

Start the generator, prints Ready to receive

print(gen.send("Hello")) # Sends "Hello", prints: Got: Hello
print(gen.send("World")) # Sends "World", prints: Got: World
The generator yields a message, then waits to receive a value via send().

The received value is assigned to the received variable and can be used inside the generator’s logic.

Use Cases
Interactive Data Processing: Generators that adjust behavior based on external input during iteration.

Coroutines: Lightweight concurrent code units that exchange data via yields and sends.

Stateful Iterations: Generators maintain internal state influenced by values sent in.

Important Points
Always prime the generator by calling next() or send(None) before sending other values.

Sending a value into a generator resumes it right after the paused yield.

If .send() is called before the first yield, a TypeError is raised.

.send(None) is equivalent to calling next() and is used to start the generator.

Narrative Content Sessions
Session 1: Introduction to .send() and Two-Way Communication
Teacher: "Today we’ll learn how generators can receive values while running. What method do you think allows us to send data into a generator?"

Student: "Is it .send()?"

Teacher: "Exactly! Unlike next(), which only resumes execution, .send(value) both resumes and sends data. But remember, before sending data, we must start the generator using next() or send(None). Let’s try a simple example together."

Session 2: Practical Usage and Common Pitfalls
Teacher: "If you call .send() before the generator starts, what do you expect?"

Student: "An error?"

Teacher: "Right, a TypeError. We must prime the generator first. Also, sending values can let the generator change behavior on the fly, which is useful in many applications like interactive workflows or co-routines."

Audio Book
Chunk Title: Two-Way Communication with Generators

Chunk Text: Generators can receive values sent into them using the .send() method. This allows external code to influence the generator while it is paused at a yield.

Example:

python
Copy
Edit
def generator():
value = yield "Start"
while True:
value = yield f"Received {value}"

gen = generator()
print(next(gen)) # Start generator
print(gen.send("Data 1")) # Send data to generator
print(gen.send("Data 2"))
Detailed Explanation:
.send() resumes the generator and sends a value inside, which the yield expression returns. This mechanism enables more interactive and stateful generators.

Analogy:
Imagine a walkie-talkie where one side speaks and listens alternately. You say something, the other side responds, then listens for your next message. Generators with .send() work similarly.

Glossary
.send(value): A method used to resume a generator and send a value into it at the paused yield.

Priming a Generator: The initial call to next() or send(None) to start generator execution up to the first yield.

Coroutine: A function that can pause and resume execution, exchanging data with the caller via yields and sends.

Estimated Study Time
15 minutes

Reference Links
Python's .send() method explained

Understanding coroutines and .send() in Python

Python Generator Send Method β€” Official Docs

PEP 342 β€” Coroutines via Enhanced Generators

Key Concepts
Generators support two-way communication using .send().

You must prime the generator before sending values.

yield expressions can both output and receive values.

This feature enables advanced control flows like coroutines and interactive generators.

Examples
Simple echo generator receiving and returning sent values.

Stateful generators changing behavior dynamically based on inputs via .send().

Flashcards
Term: What method sends data into a generator?
Definition: .send(value)

Term: How do you start a generator before sending values?
Definition: Call next() or .send(None) first to reach the first yield.

Term: What happens if you call .send(value) before the first yield?
Definition: Raises a TypeError.

Memory Aids
Rhyme: "Send a value, wake the yield, two-way talk in Python’s field!"

Story: Imagine a mailbox that not only delivers letters (yield) but also receives replies (send).

Mnemonic: S.Y.N.C. β€” Send, Yield, Next (prime), Communicate two ways.

Alternative Content
Think of a generator with .send() like a conversation β€” you say something, it listens and responds back, rather than just speaking once.


Exercises
Easy
Question: How do you send a value into a generator?

Answer: Use the .send(value) method on the generator object.

Hint: It’s like next(), but allows passing a value inside.

Question: What must you do before you can send a value to a generator?

Answer: You must first advance the generator to the first yield using next() or send(None).

Hint: The generator needs to be paused at a yield to receive a value.

Medium
Question: What does the expression received = yield x do inside a generator function?

Answer: It yields x out, and when resumed via .send(value), assigns value to received.

Hint: Think of yield as both output and input.

Question: Why might you want to send values into a generator instead of just yielding values?

Answer: To allow external code to influence the generator’s behavior dynamically, such as adjusting counters or decision logic.

Hint: This is useful in coroutines or complex iterative algorithms.

Hard
Question: Write a generator function that yields numbers starting from 0 but increments by the amount sent to it via .send(). If nothing is sent, increment by 1.

Answer: Use n = 0 initially, then inside a loop: step = yield n, and set step = 1 if step is None. Then do n += step.

Hint: Handle the case when send() is not used and default increment.

Question: Explain what happens if you call .send(value) before the generator reaches its first yield.

Answer: It raises a TypeError because the generator is not yet paused at a yield and cannot receive values. The first call must be next() or send(None).

Hint: This is why priming the generator is necessary.

Quiz
Question: What value must be sent into a generator to start it before any values can be sent?

Type: mcq

Options: None, 0, 1

Correct Answer: None

Explanation: The first call to a generator’s .send() must be .send(None) to advance it to the first yield.

Hint: This primes the generator.

Question: True or False: You can use .send() to both resume a generator and send a value to it.

Type: boolean

Options: True, False

Correct Answer: True

Explanation: .send(value) resumes the generator and sends value to the paused yield expression.

Hint: It’s a combination of next() and sending data.

Challenge Problems
Problem: Create a generator that calculates running averages. It should accept numbers sent into it and yield the current average after each number.

Solution: Keep track of the total sum and count inside the generator. On each .send(number), update sum and count, then yield the average.

Hint: Use number = yield average pattern.

Problem: Write a generator that accepts commands sent via .send() to either "add", "subtract", or "reset" an internal counter and yields the current count after each command.

Solution: Maintain an internal counter. Depending on the command sent, modify the counter and yield it.

Hint: Use command = yield count and handle each command.