Write A Python Program To Create A Function Cube Which Cube All The Elements Of An Array

Given an array containing the list of integers  your Python function should accept an array as input argument and then cube all the items present and then print it.

Array has similar features of list so let's use all the features of list for array as well.

 

 

Write A Python Program To Create A Function Cube Which Cube All The Elements Of An Array



Write A Python Program To Create A Function Cube Which Cube All The Elements Of An Array

import the required models for this Python program that is we need an array module so let's import an array.

from array import array




Define a function named as cube() passing array as an input argument to the Python function.  iterate all the items present in the array and cube  all the items result in the array assign it to the index

def cube(items):
   for index, item in enumerate(items):
       items[index] = item ** 3



In the main program take a variable named as n to store the array length. Aaj ke user to enter the array length then take another variable named as my_array  assign an Empty array containing n integers shown below “i”  signifies an integer array

Using the  for loop  to iterate until array length and ask the user to enter the array item that is an integer type then append that integer to an array.

n = int(input("Enter Array Length : "))
my_array = array("i", list())
for _ in range(n):
   temp = int(input("Enter a Array Item : "))
   my_array.append(temp)




Once we get into the Python program, let's print the original array and then call the function cube(),  after calling the cube, print the area once again so you can notice that the array has been converted to its cube.

print("Original Array is ...")
print(my_array)

print("The Cube of All Array Items is ...")
cube(my_array)
print(my_array)





Code
==================
from array import array

def cube(items):
   for index, item in enumerate(items):
       items[index] = item ** 3

n = int(input("Enter Array Length : "))
my_array = array("i", list())
for _ in range(n):
   temp = int(input("Enter a Array Item : "))
   my_array.append(temp)


print("Original Array is ...")
print(my_array)

print("The Cube of All Array Items is ...")
cube(my_array)
print(my_array)







Output
===============
Enter Array Length : 7
Enter a Array Item : 34
Enter a Array Item : 56
Enter a Array Item : 90
Enter a Array Item : 89
Enter a Array Item : 23
Enter a Array Item : 33
Enter a Array Item : 37
Original Array is ...
array('i', [34, 56, 90, 89, 23, 33, 37])
The Cube of All Array Items is ...
array('i', [39304, 175616, 729000, 704969, 12167, 35937, 50653])





Output
===============
Enter Array Length : 5
Enter a Array Item : 1
Enter a Array Item : 2
Enter a Array Item : 3
Enter a Array Item : 4
Enter a Array Item : 5
Original Array is ...
array('i', [1, 2, 3, 4, 5])
The Cube of All Array Items is ...
array('i', [1, 8, 27, 64, 125])





Conclusion
=============
Execute the above Python program by providing a random array as an input,  note down the results.

Comment it down below if you have any suggestions to improve the above Python program





Post a Comment

0 Comments