Get startedGet started for free

Write your own generator expressions

You are familiar with what generators and generator expressions are, as well as its difference from list comprehensions. In this exercise, you will practice building generator expressions on your own.

Recall that generator expressions basically have the same syntax as list comprehensions, except that it uses parentheses () instead of brackets []; this should make things feel familiar! Furthermore, if you have ever iterated over a dictionary with .items(), or used the range() function, for example, you have already encountered and used generators before, without knowing it! When you use these functions, Python creates generators for you behind the scenes.

Now, you will start simple by creating a generator object that produces numeric values.

This exercise is part of the course

Python Toolbox

View Course

Exercise instructions

  • Create a generator object that will produce values from 0 to 30. Assign the result to result and use num as the iterator variable in the generator expression.
  • Print the first 5 values by using next() appropriately in print().
  • Print the rest of the values by using a for loop to iterate over the generator object.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create generator object: result
result = ____

# Print the first 5 values
print(____)
print(____)
print(____)
print(____)
print(____)

# Print the rest of the values
for value in ____:
    print(value)
Edit and Run Code