Tuesday, 1 September 2026

πŸš€ Day 99/150 – Generator Examples in Python

 



πŸš€ Day 99/150 – Generator Example in Python

A generator is a special type of function that produces values one at a time instead of returning all values at once. It uses the yield keyword instead of return, making it memory-efficient, especially when working with large datasets.

In this post, we'll explore four common examples of generators in Python.


Method 1 – Basic Generator

Create a simple generator that yields numbers one by one.

def numbers(): yield 1 yield 2 yield 3 gen = numbers() print(next(gen)) print(next(gen)) print(next(gen))











Output
1
2
3

Explanation
  • yield returns a value and pauses the function.
  • next() resumes the generator from where it stopped.
  • Each call to next() produces the next value.

Method 2 – Generator with a Loop

Generate numbers from 1 to n.

def count(n): for i in range(1, n + 1): yield i for num in count(5): print(num)







Output
1 2 3 4 5

Explanation

  • The for loop generates numbers one by one.
  • yield returns each number individually.
  • The generator stops automatically after the last value.

Method 3 – Generator Expression

Python also provides generator expressions, which are similar to list comprehensions.

squares = (x ** 2 for x in range(1, 6)) for square in squares: print(square)




Output

1 4 9 16 25

Explanation

  • (x ** 2 for x in range(1, 6)) creates a generator expression.
  • Unlike a list comprehension, it doesn't store all values in memory.
  • Values are generated only when needed.

Method 4 – Taking User Input

Generate numbers from 1 to the number entered by the user.

def generate_numbers(n): for i in range(1, n + 1): yield i num = int(input("Enter a number: ")) for value in generate_numbers(num): print(value)












Sample Input
5

Output

1 2 3 4 5

Explanation

  • The user enters a number.
  • The generator produces numbers from 1 to that number.
  • Each value is generated only when the loop requests it.

Comparison of Methods

MethodBest For
Basic GeneratorUnderstanding yield
Generator with LoopGenerating sequences
Generator ExpressionMemory-efficient computations
User InputInteractive programs

πŸ”₯ Key Takeaways

  • A generator is a function that uses the yield keyword.
  • yield returns one value at a time and pauses the function.
  • Generators are more memory-efficient than lists because they don't store all values at once.
  • Use next() to retrieve values manually from a generator.
  • Generator expressions provide a concise way to create generators.
  • Generators are useful when working with large datasets or continuous data streams.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (342) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (351) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (90) Coursera (303) Cybersecurity (36) data (10) Data Analysis (46) Data Analytics (31) data management (16) Data Science (425) Data Strucures (18) Deep Learning (218) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Generative AI (77) Git (13) Google (54) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (396) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (34) Python (1364) Python Coding Challenge (1230) Python Library (1) Python Mathematics (15) Python Mistakes (51) Python Quiz (617) Python Tips (109) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (20) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)