PYTHON PROBLEM Define a function generatecubed numbers that

PYTHON PROBLEM

Define a function generate_cubed( numbers ) that creates and return a list where the element at each index is equal to numbers[index]**3. Your solution must use a list comprehension.

Example Usage:
numbers = range( 7 )
print generate_cubed( numbers )
Run > Run Module
[0, 1, 8, 27, 64, 125, 216]

Solution

Please find the required program along with its output. Please see the comments against each line to understand the step.

def generate_cubed( numbers ):   #function to generate cubes
   for i, val in enumerate(numbers):   #iterate over the list
       numbers[i] = val * val * val   #set the cube of val to the current i
   return numbers   #return the altered list

numbers = [0,1,2,3,4,5,6]
print (generate_cubed( numbers ))

---------------------------------------------------------

OUTPUT:

PYTHON PROBLEM Define a function generate_cubed( numbers ) that creates and return a list where the element at each index is equal to numbers[index]**3. Your so

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site