Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Python Fundamentals

Lesson 31 of 55 · Python

Generators and Iterators

Duration: 20 minutes

Generators and Iterators\n\nGenerators produce items lazily using the yield keyword, turning a regular function into an iterator that emits values on demand.\n\npython\ndef fib(limit):\n a, b = 0, 1\n while a < limit:\n yield a\n a, b = b, a + b\n\nfor n in fib(20):\n print(n, end=' ')\n# Output: 0 1 1 2 3 5 8 13\n\n\nGenerators are memory‑efficient for large data streams because they never store the whole sequence at once.\n

Previous: DecoratorsNext: Context Managers