w3resource

Python Generators and Yield Exercises, Practice, Solution

Python Generators and Yield [ 17 exercises with solution ]

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

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.

1. 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. Write a Python program to implement a generator that generates random numbers within a given range.
Click me to see the sample solution

3. 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. Write a Python program to implement a generator function that generates the Fibonacci sequence.
Click me to see the sample solution

5. 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. 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. 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. 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. 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. 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. Write a Python program to create a generator that generates all possible permutations of a string.
Click me to see the sample solution

12. 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. 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. 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. 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. 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. 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



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/generators-yield/index.php