Defining a generator

Report a typo

Let my_list be a list of some natural numbers.

Consider the pieces of code below. Which of them are the correct ways to define a generator producing the cubes of the numbers in my_list?

A.

my_generator = (n ** 3 for n in my_list)

B.

my_generator = [n ** 3 for n in my_list]

C.

def my_generator(my_list):
    for num in my_list:
        yield num**3

D.

for num in my_list:
    print(num**3)

E.

def my_generator(my_list):
    for num in my_list:
        return num**3
Select one or more options from the list
___

Create a free account to access the full topic