Generators
Source: 9-Advance Python Concepts/9.2-Generators.ipynb
Start here — no coding background needed
What you will learn
Create lazy sequences with `yield` — efficient loops.
In simple words
Generator produces values one by one instead of building a huge list in memory.
Power tools for later — iterators, generators, decorators. Skim if new; revisit anytime.
Easy example — run this first. Change values and press Run again.
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Reference notes (from full bootcamp)
Optional — deeper detail for when you are ready
Generators
Generators are a simpler way to create iterators. They use the yield keyword to produce a series of values lazily, which means they generate values on the fly and do not store them in memory.
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook): <generator object square at 0x00000222FFEFF2A0>
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook): 0 1 4
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook): <generator object square at 0x00000222FFEFE8E0>
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook): <generator object my_generator at 0x00000222FFD95DD0>
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Expected (from notebook): 1 2 3
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Practical Example: Reading Large Files
Generators are particularly useful for reading large files because they allow you to process one line at a time without loading the entire file into memory.
### Practical : Reading LArge Files
def read_large_file(file_path):
with open(file_path,'r') as file:
for line in file:
yield lineBrowser practice only — full example needs Python on your computer (files, Flask, threads, etc.).
Expected (from notebook): Smt. Droupadi Murmu was sworn in as the 15th President of India on 25 July, 2022. Previously, she was the Governor of Jharkhand from 2015 to 2021. She has devoted her life to empowering the downtrodden and the marginalised sections and deepening the democratic values. Early Life and Education Born in a Santhali tribal family on 20 June, 1958 at Uparbeda village, Mayurbhanj, Odisha, Smt. Murmu’s early life was marked by hardships and struggle. On completion of primary education from the village school, she went to Bhubaneswar on her own initiative to continue her studies. She earned the degree of Bachelor of Arts from Ramadevi Women’s College, Bhubaneswar and became the first woman from her village to receive college education. Professional Career From 1979 to 1983, Smt. Murmu served as a Junior Assistant in the Irrigation and Power Department, Government of Odisha. Later, she served as an honorary teacher at Sri Aurobindo Integral Education Centre, Rairangpur, from 1994 to 1997. Public Life In 2000, Smt. Murmu was elected from the Rairangpur constituency as a Member of the Legislative Assembly of Odisha and continued to hold the post till 2009, serving two terms. During this period, she served as Minister of State (Independent Charge), Department of Commerce and Transport in the Government of Odisha from March 6, 2000 to August 6, 2002 and as Minister of State (Independent Charge), Department of Fisheries and Animal Resources Development, Government of Odisha from Augu
Runs in your browser via Pyodide — no server. First run may take a few seconds.
Conclusion
Iterators and generators are powerful tools in Python for creating and handling sequences of data efficiently. Iterators provide a way to access elements sequentially, while generators allow you to generate items on the fly, making them particularly useful for handling large datasets and infinite sequences. Understanding these concepts will enable you to write more efficient and memory-conscious Python programs.
Practice test — try yourself
Write code, press Check. Wrong answer shows the correct code to copy & run.
You learned "Generators". Use print() to show: Done: Generators
Hint: Use one print() with the exact text.