w3resource

Python Generators and Yield Exercises, Practice, Solution


This resource offers a total of 85 Python Generators and Yield problems for practice. It includes 17 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

Iterables: An Iterable is basically an object that any user can iterate over (read its items one by one).

Generators: Generators are iterators, a kind of iterable we can only iterate over once. Generators do not store all the values in memory, they generate the values on the fly.

Yield: yield is a keyword that is used like return, except the function will return a generator.

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. Cubes Generator

Write a Python program that creates a generator function that yields cubes of numbers from 1 to n. Accept n from the user.

Click me to see the sample solution


2. Random Number Generator

Write a Python program to implement a generator that generates random numbers within a given range.

Click me to see the sample solution


3. Prime Generator (Range)

Write a Python program that creates a generator function that generates all prime numbers between two given numbers.

Click me to see the sample solution


4. Fibonacci Generator

Write a Python program to implement a generator function that generates the Fibonacci sequence.

Click me to see the sample solution


5. List Permutations Generator

Write a Python program to implement a generator function that generates all permutations of a given list of elements.

Click me to see the sample solution


6. Combinations Generator

Write a Python program to implement a generator that yields all possible combinations of a given list of elements.

Click me to see the sample solution


7. Collatz Sequence Generator

Write a Python program to implement a generator that generates the Collatz sequence for a given number.

Click me to see the sample solution


8. Next Palindrome Generator

Write a Python program that creates a generator function that generates the next palindrome number after a given number.

Click me to see the sample solution


9. Prime Factors Generator

Write a Python program that creates a generator that generates all prime factors of a given number.

Click me to see the sample solution


10. Prime Generator (Duplicate Variation)

Write a Python program that creates a generator function that generates all prime numbers between two given numbers.

Click me to see the sample solution


11. String Permutations Generator

Write a Python program to create a generator that generates all possible permutations of a string.

Click me to see the sample solution


12. Next Happy Number Generator

Write a Python program to implement a generator that generates the next happy number after a given number.

Click me to see the sample solution


13. Square and Cube Roots Generator

Write a Python program to create a generator that generates the square, cube roots of numbers from 1 to n.

Click me to see the sample solution


14. Next Armstrong Number Generator

Write a Python program to implement a generator that generates the next Armstrong number after a given number.

Click me to see the sample solution


15. Factors Generator

Write a Python program to create a generator function that generates all factors of a given number.

Click me to see the sample solution


16. Running Average Generator

Write a Python program to implement a generator function that generates the running average of a sequence of numbers.

Click me to see the sample solution


17. Powers Generator

Write a Python program to create a generator function that generates the powers of a number up to a specified exponent.

Click me to see the sample solution


Python Code Editor:

More to Come!

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.