Python Program To Replace The Elements of List With its Cube

 

In this post, we are going to write a python program to replace the elements of a list with its cube.




General Idea :

To replace the elements of the list with its cube, we are going to use the list transversing method. We are going to create two empty lists. In first, we are going to input the elements from the user. In second, we are going to fill the elements by the cube of the respective element which is stored in the first list.

Pre-Requisites :

  • Python Basics
  • For loop
  • List Manipulation
Program :

n=int(input("Enter the number of elements :"))
l=[]
cube=[]
for i in range(0,n):
    x=int(input("Enter the element "))
    l.append(x)
for i in l:
    a=i**3
    cube.append(a)
print(cube)
























Explanation :



1.In this line, we are inputting the number of elements of the list using the input function and storing that to identifier n.




2.Now we are creating an empty list, for inputting the elements for which we need to replace the cube.





3.Now we are creating another empty list, for storing the cube of the elements inputted by the user.




4.Here we are using the for loop to input the elements of the list. There should be n elements. That means the for loop should repeat n times. Hence the start value is 0 and the end value is n.





5.This statement is written under the for loop created in step 4. Here we are inputting the elements for which we need to find the cube. Here we are storing the number inputted by the user and storing that to x. That means as the for loop enters a new step, the value of x changes.




6. This statement is also written under the for loop created in step 4. As soon as the user inputs the number, that will be stored in x. Now are appending the element entered to empty list l which is created n step 2. Appending the element means, storing the element to the list.




7.This statement is written separately apart from the for loop created in step 4. Now we are creating another for loop to transverse the list of inputted numbers. Here transversing means, accessing the elements of the list. If list is [1,2,3]. Then 'i' will be 1 in the first step, 2 in the second step, and 3 in the last step.




8. This step is written under the for loop created in step 7. Here we are making the elements into its cube. The operator used in this step is (**) exponential operator. If x**y means x raised to the power y. For example, 2**3 is equal to 8. We are storing the cube to identifier a. As the i value changes, correspondingly a value changes.If list is [1,2,3]. Then 'i' will be 1 in the first step and a will be 1, 2 in the second step and a will be 4, and 3 in the last step and a will be 27.




9. This statement is also written under the for loop created in step 7. Here we are appending 'a' in the empty list cube which was created in step 3. After the loop ends, the list will contain the cubes of the number entered by the user.




10. This statement is written separately apart from any for loop. Here we are printing the list cube using the print function.

Input/Output :



















Thank You





Comments

Popular posts from this blog

Python Program to Print Fibonacci series - Codicaly

Python - Program to find the given number is a prime or composite number

Python Program To Print Average